当我从 Amazon S3 下载 ZIP 文件并使用 Java 提取它时,它不会保留 ZIP 内文件的原始时间戳。 为什么?这是解压缩的 Java 代码:
public void unzipFile(String zipFile, String newFile) {
try {
FileInputStream fis = new FileInputStream(zipFile);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream zis = new ZipInputStream(bis);
FileOutputStream fos = new FileOutputStream(newFile);
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = zis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
//close resources
fos.close();
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
基本上,我想要 zip 文件内文件的时间戳,假设文件 X 需要保留 JAN-01-2010。但文件 X 被 ZIP 文件的时间戳覆盖,该文件的时间戳为 SEP-20-2013。
最佳答案
这是因为您要将 Zip 文件的内容放入新文件中。
你可以尝试这样的事情:
public void unzipFile(String zipFile, String outputFolder){
try {
byte[] buffer = new byte[1024];
File folder = new File(outputFolder);
if(!folder.exists()){
folder.mkdir();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
newFile.setLastModified(ze.getTime());
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
提取自:http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/通过修改添加修改时间。
关于java - 在 Java 中提取时,从 Amazon S3 下载 ZIP 不会保留时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18985518/