java - 在 Java 中提取时,从 Amazon S3 下载 ZIP 不会保留时间戳

标签 java amazon-s3

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

相关文章:

java - 环境变量是否传递到从取消配置的 azure vm(linux) 镜像创建的新规模集实例

java - 使用超时和信号量进行线程阻塞

javascript - Promise 中的 S3 下载到 FileStream?

django - 如何使用 Amazon S3 和 Boto 在 Django 中仅为经过身份验证的用户提供上传的文件?

java - Java 中 map 中的重复条目

java - Dropbox V2 uploadBuilder : "String ' path' does not match pattern"

java - 在main中调用方法

reactjs - 在 React 中使用 aws-amplify 将文件上传到具有联合身份的 S3 时出错

amazon-web-services - 从 S3 读取许多小文件非常慢

.net - 哪个 Amazon S3 .NET 库最有用且最高效?