1

当我从 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。

4

1 回答 1

1

这是因为您将 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/带有修改以添加修改时间。

于 2013-09-24T15:25:02.287 回答