Recommended software development tools: zeroturnaround zip class library

Source: Internet
Author: User
Tags unpack

Producer class library, which is the Apache commons compress class library on the Apache website, or Chilkat
Java zip class library, but in general, these class libraries provide low-level APIs, which are not very convenient to operate. Today we recommend zeroturnaround (zt-zip for short) the compression class library features convenience and simplicity. We can compare it. If you use the standard Java class library to compress all the files in a directory, you need to write the code like this:

File dir = new File("demo");ZipOutputStream out = new ZipOutputStream(new FileOutputStream("demo.zip"));try {  File[] files = dir.listFiles();  for (int i = 0; i < files.length; i++) {    File file = files[i];    ZipEntry entry = new ZipEntry(file.getName());    entry.setSize(file.length());    entry.setTime(file.lastModified());    out.putNextEntry(entry);    FileInputStream in = new FileInputStream(file);    try {      IOUtils.copy(in, out);    }    finally {      IOUtils.closeQuietly(in);    }    out.closeEntry();   } } finally {   IOUtils.closeQuietly(out);}

Using the ZT-zip toolkit, your code becomes one line:

ZipUtil.pack(new File("demo"), new File("demo.zip"));

You do not need to close the file data stream by yourself. The interface of this class library automatically performs this for you.

People who often do Java compression programming may mention another compression Class Library: truezip, which is also a very good class library, and one advantage of ZT-zip over it is: memory consumption is very small, because truezip uses a lot of VM heap memory, while ZT-zip only operates in the form of data streams, of course, this is also the function provided by the ZT-zip API. It is not truezip.
The reason why the API is so common.

You can download this class library from GitHub.

 

Examples

//Unpacking//Check if an entry exists in a ZIP archiveboolean exists = ZipUtil.containsEntry(new File("/tmp/demo"), "foo.txt");//Extract an entry from a ZIP archive into a byte arraybyte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt");//Extract an entry from a ZIP archive into file systemZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("/tmp/bar.txt"));//Extract a ZIP archiveZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"));//Extract a ZIP archive which becomes a directoryZipUtil.explode(new File("/tmp/demo.zip"));//Extract a directory from a ZIP archive including the directory nameZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() {  public String map(String name) {    return name.startsWith("doc/") ? name : null;  }});//Extract a directory from a ZIP archive excluding the directory namefinal String prefix = "doc/"; ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() {  public String map(String name) {    return name.startsWith(prefix) ? name.substring(prefix.length()) : name;  }});//Print .class entry names in a ZIP archiveZipUtil.iterate(new File("/tmp/demo.zip"), new ZipInfoCallback() {  public void process(ZipEntry zipEntry) throws IOException {    if (zipEntry.getName().endsWith(".class"))      System.out.println("Found " + zipEntry.getName());  }});//Print .txt entries in a ZIP archive (uses IoUtils from Commons IO)ZipUtil.iterate(new File("/tmp/demo.zip"), new ZipEntryCallback() {  public void process(InputStream in, ZipEntry zipEntry) throws IOException {    if (zipEntry.getName().endsWith(".txt")) {      System.out.println("Found " + zipEntry.getName());      IOUtils.copy(in, System.out);    }  }});

 

//Packing//Compress a directory into a ZIP archiveZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"));//Compress a directory which becomes a ZIP archiveZipUtil.unexplode(new File("/tmp/demo.zip"));//Compress a directory into a ZIP archive with a parent directoryZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"), new NameMapper() {  public String map(String name) {    return "foo/" + name;  }});//Add an entry from file to a ZIP archiveZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("f/tmp/oo.txt"), new File("/tmp/new.zip"));//Add an entry from byte array to a ZIP archiveZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));//Add an entry from file and from byte array to a ZIP archiveZipEntrySource[] entries = new ZipEntrySource[] {    new FileSource("doc/readme.txt", new File("foo.txt")),    new ByteSource("sample.txt", "bar".getBytes())};ZipUtil.addEntries(new File("/tmp/demo.zip"), entries, new File("/tmp/new.zip"));//Replace a ZIP archive entry from fileboolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("/tmp/foo.txt"), new File("/tmp/new.zip"));//Replace a ZIP archive entry from byte arrayboolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));//Replace a ZIP archive entry from file and byte arrayZipEntrySource[] entries = new ZipEntrySource[] {    new FileSource("doc/readme.txt", new File("foo.txt")),    new ByteSource("sample.txt", "bar".getBytes())};boolean replaced = ZipUtil.replaceEntries(new File("/tmp/demo.zip"), entries, new File("/tmp/new.zip"));

 

//Comparison//Compare two ZIP archives (ignoring timestamps of the entries)boolean equals = ZipUtil.archiveEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"));//Compare two ZIP archive entries with same name (ignoring timestamps of the entries)boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo.txt");//Compare two ZIP archive entries with different names (ignoring timestamps of the entries)boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo1.txt", "foo2.txt");

 

 

Note: This article is reproduced from:

Http://www.aqee.net/development-tools-zt-zip/

Https://github.com/zeroturnaround/zt-zip

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.