代码压缩目录中的文件,但不压缩子目录

时间:2014-04-02 12:40:20

标签: java

我写了一个代码来压缩所选目录中的文件,但是它没有在子目录中搜索文件,我找到了一个我插入的循环,但似乎仍然没有用。

如果有人可以帮助我修改我的代码,那么它会搜索插入路径中的所有文件夹。

请参阅以下代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.JOptionPane;


 public class List {
 private static void getFiles(File folder, java.util.List<File> list) throws          FileNotFoundException, IOException {  

  String v_days; // number of days inserted
  v_days = JOptionPane.showInputDialog("Enter duration of days back that should be archived? (Numerical Value)");  

folder.setWritable(true);
    File[] files = folder.listFiles();
    long purgeTime = System.currentTimeMillis() - ((long)Integer.parseInt(v_days) * 24 * 60 * 60 * 1000); //Calculate how old files must be before they get archived
   try {
      for (int j = 0; j < files.length; j++) {
      if (files[j].lastModified() < purgeTime  ) {
        list.add(files[j]);
        if (files[j].isDirectory()) {
                getFiles(files[j], list);

        {


  byte[] buffer = new byte[4096]; // create byte buffer

  String zipFileName = files[j].getAbsolutePath() + ".zip" ; //Create the final zip name of the file
  String zipFileFullPath = files[j].getAbsolutePath() ; //Get the full path of the normal file before being zipped

  System.out.println("Starting with file: " + zipFileFullPath ); 
  System.out.println("File name will be: " + files[j].getAbsolutePath() + ".zip") ;

  FileOutputStream fos = new FileOutputStream(zipFileName); //Add the final zip name to the output stream

  ZipOutputStream zos = new ZipOutputStream(fos); //Add the final zip name to the output stream

  FileInputStream fis = new FileInputStream(files[j]); //Add the current file for the iteration into the input stream

  zos.putNextEntry(new ZipEntry(files[j].getName())); //Adds the filename of the current file for the iteration into the zip file

  int length;

  while ((length = fis.read(buffer)) > 0) //While there is anything in the buffer, write   it
  {

   zos.write(buffer, 0, length);

  }

  zos.closeEntry(); //Close the entry into the output stream


  fis.close(); //Close the input stream


  zos.close(); //Close the output stream

  System.out.println("Done with file: " + zipFileFullPath) ;

     try
      {

        boolean success = (new File(zipFileFullPath)).delete();
        if ((success) && (files[j].lastModified() < purgeTime))//If file zipped successfully and the file is older than days inserted, then delete
            {
             System.out.println("The files has been successfully deleted"); 
             }
        else 
        { 
            System.out.println("Error occurred, could not delete files");
        }
             }
              catch(Exception e)
              {
               e.printStackTrace();
              }   
          }


         }
      }

   }
    catch (IOException ioe) //Exception Handling
        {
    System.out.println("Error creating zip file" + ioe);  
       }
   }
 }     
    public static void main(String[] args) throws IOException {
    File folder = new File("C:\\Users\\kroon_000\\Desktop\\test.zip");
    java.util.List<File> list = new ArrayList<File>();
    getFiles(folder, list);   
 }
   }

1 个答案:

答案 0 :(得分:0)

由于格式化,您的代码很难阅读,但this page显示您想要的内容并使用Apache Commons库来执行此操作。