Java:维护压缩文件修改日期 [英] Java: Maintaining zipped files Modified Date

查看:44
本文介绍了Java:维护压缩文件修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的一个专有程序会在解压缩时压缩并提取某些文件,而不会更改文件的修改日期.我还根据我们程序中的源代码创建了自己的 zip 和提取工具,但是当我解压缩文件时,所有压缩文件的修改日期都显示为解压缩时间 &日期.这是我的提取代码:

A proprietary program that I'm working with zips up and extracts certain files without changing the modified date of the files when unzipping. I'm also creating my own zip and extraction tool based off the source code in our program but when I'm unzipping the files the modified date of all zipped files is showing with the unzip time & date. Here's the code for my extraction:

public static int unzipFiles(File zipFile, File extractDir) throws Exception 
{
        int totalFileCount = 0;
        String zipFilePath = zipFile.getPath();

        System.out.println("Zip File Path: " + zipFilePath);

        ZipFile zfile = new ZipFile(zipFile); 
        System.out.println("Size of ZipFile: "+zfile.size());

        Enumeration<? extends ZipEntry> entries = zfile.entries(); 

        while (entries.hasMoreElements()) 
        { 
          ZipEntry entry = entries.nextElement(); 
          System.out.println("ZipEntry File: " + entry.getName());
          File file = new File(extractDir, entry.getName()); 
          if (entry.isDirectory()) 
          { 
            System.out.println("Creating Directory");
            file.mkdirs(); 
          } 
          else 
          { 
            file.getParentFile().mkdirs(); 
            InputStream in = zfile.getInputStream(entry); 
            try 
            { 
              copy(in, file); 
            } 
            finally 
            {   
              in.close(); 
            } 
          } 
          totalFileCount++;
        }
        return totalFileCount; 
      } 

private static void copy(InputStream in, OutputStream out) throws IOException 

{

  byte[] buffer = new byte[1024]; 
  System.out.println("InputStream/OutputStram copy");
  while (true) 
  { 
    int readCount = in.read(buffer); 
    if (readCount < 0) 
    { 
      break; 
    } 
    out.write(buffer, 0, readCount); 
   } 
} 

我确信除了进行输入流/输出流复制之外,还有更好的方法可以做到这一点.我确定这是罪魁祸首,因为使用 winRAR 进行提取不会更改我压缩文件的日期.

I'm sure there is a better way to do this other than doing the inputstream/outputstream copy. I'm sure this is the culprit as doing an extraction with winRAR does not change the date with the files I zipped.

推荐答案

使用 ZipEntry.getTime 获取最后修改时间,使用 File.setLastModified 在完成复制后在文件上设置它.

Use ZipEntry.getTime to get the last-modified time and File.setLastModified to set it on the file after you are done copying it.

这篇关于Java:维护压缩文件修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆