关于 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: 2014-01-12-17-37-28-11304-Processed_INST-11304-20140112-120140112.zip, Date:        Sun Jan 12 17:37:29 CAT 2014
    File: Bill Issuer Summary - 2014-01-23 to 2014-01-23.pdf, Date: Fri Jan 24 02:18:39 CAT 2014
    File: Cape Agulhas Settlement for 2013-12-02 to 2013-12-02.csv, Date: Tue Dec 03 09:24:06 CAT 2013
    File: Cape Agulhas Settlement for 2013-12-02 to 2013-12-02.pdf, Date: Tue Dec 03 09:24:06 CAT 2013
    File: Cape Agulhas Store Trans for 2013-12-02 to 2013-12-02.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();
    }
}