关于 java:如何从我的结果中压缩文件?

How to zip files from my result?

本问题已经有最佳答案,请猛点这里访问。

我需要知道如何压缩从结果中获得的文件,我文件夹中的所有文件都比 1 月 30 日旧,我需要压缩它们,我该怎么办??

请看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
       package agefilefilter;

       import java.io.File;
       import java.io.FileFilter;
       import java.io.IOException;
       import java.util.Date
       import java.util.GregorianCalendar;

       import org.apache.commons.io.filefilter.AgeFileFilter;

 public class AgeFileFilterTest {

   public static void main(String[] args) throws IOException {

      File directory = new File("C:////Users////kroon_000////Desktop////files");

      GregorianCalendar cal = new GregorianCalendar();
      cal.set(2014, 0, 30, 0, 0, 0); // January 30th, 2014
      Date cutoffDate = cal.getTime();

      System.out.println("/
Before"
+ cutoffDate);
      displayFiles(directory, new AgeFileFilter(cutoffDate));

      }

     public static void displayFiles(File directory, FileFilter fileFilter) {
      File[] files = directory.listFiles(fileFilter);
      for (File file : files) {
      Date lastMod = new Date(file.lastModified());
      System.out.println("File:" + file.getName() +", Date:" + lastMod +"");
      }
     }

    }

结果:

1
2
3
4
5
6
    Before Thu Jan 30 00:00:00 CAT 2014
    File: 2014011217372811304Processed_INST1130420140112120140112.zip, Date:        Sun Jan 12 17:37:29 CAT 2014
    File: Bill Issuer Summary 20140123 to 20140123.pdf, Date: Fri Jan 24 02:18:39 CAT 2014
    File: Cape Agulhas Settlement for 20131202 to 20131202.csv, Date: Tue Dec 03 09:24:06 CAT 2013
    File: Cape Agulhas Settlement for 20131202 to 20131202.pdf, Date: Tue Dec 03 09:24:06 CAT 2013
    File: Cape Agulhas Store Trans for 20131202 to 20131202.csv, Date: Tue Dec 03 09:24:09 CAT 2013


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void zip(String[] files, String zipFile) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
    try {
        byte data[] = new byte[BUFFER_SIZE];

        for (int i = 0; i < files.length; i++) {
            FileInputStream fi = new FileInputStream(files[i]);    
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != 1) {
                    out.write(data, 0, count);
                }
            }
            finally {
                origin.close();
            }
        }
    }
    finally {
        out.close();
    }
}


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/267593.html

(0)
上一篇 2022年6月18日
下一篇 2022年6月18日

相关推荐

发表回复

登录后才能评论