
ZIP अभिलेख हेरफेर के लिए सुविधाजनक विधियाँ प्रदान करने वाली जावा लाइब्रेरी, जिसमें पैकिंग, अनपैकिंग, प्रविष्टि पुनरावृत्ति, रूपांतरण और तुलना शामिल है।
यह परियोजना Rein Raudjärv द्वारा शुरू और कोडित की गई थी जब उन्हें LiveRebel आंतरिक प्रक्रियाओं के लिए बड़ी संख्या में बड़े ZIP संग्रहों को संसाधित करने की आवश्यकता थी। इसके तुरंत बाद हमने इस उपयोगिता का उपयोग अन्य परियोजनाओं में करना शुरू कर दिया क्योंकि यह उपयोग में आसान थी और यह बस काम करती थी।
यह परियोजना स्ट्रीम-आधारित एक्सेस के लिए java.util.zip.* पैकेज का उपयोग करके बनाई गई है। फ़ाइल सिस्टम उपयोग के लिए अधिकांश सुविधाजनक विधियाँ भी समर्थित हैं।
परियोजना के आर्टिफैक्ट Maven Central Repository में उपलब्ध हैं। इसे अपने maven प्रोजेक्ट में शामिल करने के लिए आपको डिपेंडेंसी निर्दिष्ट करनी होगी।
...
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-zip</artifactId>
<version>1.11</version>
<type>jar</type>
</dependency>
...
ध्यान दें कि 1.8 अंतिम Java 1.4 संगत रिलीज़ है। तब से Java 1.5 आवश्यक है।
निम्नलिखित कार्यात्मक आवश्यकताएँ थीं:
और ये गैर-कार्यात्मक आवश्यकताएँ:
boolean exists = ZipUtil.containsEntry(new File("/tmp/demo.zip"), "foo.txt");
byte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt");
byte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", Charset.forName("IBM437"));
ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("/tmp/bar.txt"));
ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"));
ZipUtil.explode(new File("/tmp/demo.zip"));
ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() {
public String map(String name) {
return name.startsWith("doc/") ? name : null;
}
});
final 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;
}
});
ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() {
public String map(String name) {
if (name.contains("/doc")) {
return name;
}
else {
// returning null from the map method will disregard the entry
return null;
}
}
});
ZipUtil.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());
}
});
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);
}
}
});
ZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"));
ZipUtil.unexplode(new File("/tmp/demo.zip"));
ZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"), new NameMapper() {
public String map(String name) {
return "foo/" + name;
}
});
ZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("f/tmp/oo.txt"), new File("/tmp/new.zip"));
ZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));
ZipEntrySource[] 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"));
ZipEntrySource[] entries = new ZipEntrySource[] {
new FileSource("doc/readme.txt", new File("foo.txt")),
new ByteSource("sample.txt", "bar".getBytes())
};
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("/tmp/new.zip")));
ZipUtil.addEntries(new File("/tmp/demo.zip"), entries, out);
}
finally {
IOUtils.closeQuietly(out);
}
boolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("/tmp/foo.txt"), new File("/tmp/new.zip"));
boolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));
ZipEntrySource[] 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"));
ZipEntrySource[] addedEntries = new ZipEntrySource[] {
new FileSource("/path/in/zip/File1.txt", new File("/tmp/file1.txt")),
new FileSource("/path/in/zip/File2.txt", new File("/tmp/file2.txt")),
new FileSource("/path/in/zip/File3.txt", new File("/tmp/file2.txt")),
};
ZipUtil.addOrReplaceEntries(new File("/tmp/demo.zip"), addedEntries);
boolean transformed = ZipUtil.transformEntry(new File("/tmp/demo"), "sample.txt", new StringZipEntryTransformer() {
protected String transform(ZipEntry zipEntry, String input) throws IOException {
return input.toUpperCase();
}
}, new File("/tmp/demo.zip"));
boolean equals = ZipUtil.archiveEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"));
boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo.txt");
boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo1.txt", "foo2.txt");
यह लाइब्रेरी slf4j-api लॉगिंग फ्रेमवर्क का उपयोग कर रही है। सभी लॉग स्टेटमेंट या तो DEBUG या TRACE स्तर पर हैं। आप जिस लॉगिंग फ्रेमवर्क का उपयोग कर रहे हैं, उसके आधार पर एक सरल -Dorg.slf4j.simpleLogger.defaultLogLevel=debug या System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug"); zt-zip लाइब्रेरी से लॉग स्टेटमेंट दिखाने का काम करेगा। आप अपने लॉगिंग फ्रेमवर्क के साथ प्रति पैकेज स्तरों और लॉग संदेशों के शामिल करने को और अधिक ठीक कर सकते हैं।