
ZIP 아카이브 조작을 위한 편의 메서드를 제공하는 Java 라이브러리로, 압축, 압축 해제, 항목 반복, 변환 및 비교 기능을 포함합니다.
이 프로젝트는 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 라이브러리의 로그 문을 표시할 수 있습니다. 로깅 프레임워크를 사용하여 패키지별 로그 메시지의 수준과 포함을 더 세밀하게 조정할 수 있습니다.