Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java #100

Open
magicdawn opened this issue May 20, 2017 · 2 comments
Open

Java #100

magicdawn opened this issue May 20, 2017 · 2 comments
Labels

Comments

@magicdawn
Copy link
Owner

Java

@magicdawn
Copy link
Owner Author

magicdawn commented May 20, 2017

demos

write apk

http://stackoverflow.com/questions/2223434/appending-files-to-a-zip-file-with-java

  • nio FileSystem 比较方便, 无需手动复制
package ml.magicdawn.local;

import java.io.Writer;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by magicdawn on 2017/5/21.
 */
public class ApkPacker {
  public ApkPacker() {
  }

  public  static void main(String[] args) throws Exception {
    String home = System.getenv("HOME");
    String file = home + "/workspace/fengjr/android_pack/in-java.apk";

    Map<String, String> env = new HashMap<>();
    env.put("create", "true");

    Path zipfile = Paths.get(file);
    URI uri = URI.create("jar:file:" + file);

    try(FileSystem fs = FileSystems.newFileSystem(uri, env, null)) {
      Path nf = fs.getPath("META-INF/new.txt");
      try (Writer writer = Files.newBufferedWriter(nf, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
        writer.write("hello world" + new Date().toString());
      } catch (Exception e) {
        e.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

@magicdawn
Copy link
Owner Author

magicdawn commented May 13, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant