关于 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
  • stackoverflow.com/questions/9324933/…检查这个java zip库
  • 您使用的是 Java 7 吗?
  • 谢谢,我会看看@pomkine
  • 您可以使用 java.util.zip.ZipFile 包对其进行压缩。请找到有用的链接。
  • 如果您使用的是 Java 7,请改用 FileSystem。更容易使用。


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();
    }
}


来源:https://www.codenong.com/22605151/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9806.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?