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

How to compress a single file instead of a directory #103

Open
hncc opened this issue May 11, 2018 · 3 comments
Open

How to compress a single file instead of a directory #103

hncc opened this issue May 11, 2018 · 3 comments

Comments

@hncc
Copy link

hncc commented May 11, 2018

i want to such as
ZipUtil.pack(“tmp/demo.txt”), new File("tmp/demo.zip"))
Instead of ZipUtil.pack(“tmp/demo”), new File("tmp/demo.zip"))

can u help me??

@sidgek
Copy link

sidgek commented Jun 20, 2018

If you need a simple way to zip a single file, you can create a method with something like this:

File zip = new File("tmp/demo.zip");
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(zip.toPath()))) {
  // Pass through to create empty zip archive.
}
ZipUtil.addEntry(zip, "tmp/demo.txt", new File("tmp/demo.txt"));

Don't think the above should be added to the library, just figured I'd offer a possible workaround.

@4asifm
Copy link

4asifm commented Sep 26, 2018

The ZipUtil.addEntry line fails with
Exception in thread "main" org.zeroturnaround.zip.ZipException: java.util.zip.ZipException: zip file is empty

java 1.8

@github24518
Copy link

    //1.create ZipEntrySource array,one or more files add to array.
    List<ZipEntrySource> files = new ArrayList<>();
    files.add(new FileSource("1.txt", new File("/tmp/1.txt")));
    files.add(new FileSource("2.txt", new File("/tmp/2.txt")));
    ZipEntrySource[] array = files.toArray(new ZipEntrySource[files.size()]);
   
    //2.pack
    ZipUtil.pack(array, new File( "/data/zip/demo.zip"));

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

No branches or pull requests

4 participants