-1

I need to know how can I zip the files I get from my result, all the files are older that 30 Jan in my folder and I need to zip them, what can I do??

Please see code:

       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("\nBefore " + 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 + "");
      }
     }

    }

Result:

    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
Dirk K
  • 9
  • 3

1 Answers1

1
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();
    }
}
Lucian Novac
  • 1,255
  • 12
  • 18