I가 완봉 및 압축 해제를 위해 잘 작동하는이 :자바, 같은
public static final String SLASH = "/";
public static void zip(int level, File zipfile, File... files) throws IOException {
try (FileOutputStream fo = new FileOutputStream(zipfile); ZipOutputStream zo = new ZipOutputStream(fo)) {
zo.setLevel(level);
for (File file : files) {
zip("", file, zo);
}
zo.flush();
zo.finish();
zo.close();
// !!!!!!!! Tried these !!!!!!!!!
long time = Cal.date(1970, Cal.Month.JANUARY, 1).getTime();
zipfile.setLastModified(time);
Files.setAttribute(zipfile.toPath(), "lastAccessTime", FileTime.fromMillis(time));
}
}
public static File unzip(File zipfile, File to) throws IOException {
ZipFile filezipped = new ZipFile(zipfile);
Enumeration<? extends ZipEntry> entries = filezipped.entries() ;
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement() ;
File file = new File(to, zipEntry.getName());
if (zipEntry.isDirectory()) {
file.mkdirs();
}
else {
file.getParentFile().mkdirs();
try (InputStream in = filezipped.getInputStream(zipEntry)) {
Files.copy(in, file.toPath());
}
}
}
return zipfile;
}
private static void zip(String base, File file, ZipOutputStream zo) throws IOException {
String path = base + file.getName();
if (file.isDirectory()) {
path += SLASH;
zo.putNextEntry(new ZipEntry(path));
for (File ff : file.listFiles()) {
zip(path, ff, zo);
}
zo.closeEntry();
} else {
zo.putNextEntry(new ZipEntry(path));
Files.copy(file.toPath(), zo);
zo.closeEntry();
}
}
그러나, zip 파일도의 내용하지만 다른 검사를해야합니다 파일은 변경되지 않습니다. git을 사용하여 파일을 커밋 할 때 문제가됩니다.
내용이 변경하지 않는 한 어떻게 압축 파일이 동일한 모든 시간을 유지하도록 할 수 있습니다?
는 압축에있는 파일의 타임 스탬프에이 변경 있습니까? –
흠, 아니요. 그러나 그것은 내가 압축 해제에 관해 생각하게했다. 압축이 풀린 후 다시 압축됩니다. 아마도 압축을 풀면 헤더 불일치가 발생합니다. 나는 시도 할 것이다. – momomo
@RobbyCornelissen 아니, 내 대답은 아래에서와 같이'ZipEntry' 객체의 시간을 설정할 때 여전히 심지어 다른 MD5 – momomo