2014-04-02 152 views
0

我寫了一個代碼來壓縮選定目錄中的文件,但它不搜索子目錄中的文件,我找到了一個插入的循環,但似乎仍然無法工作。代碼在目錄但不是子目錄中的zip文件

如果有人可以請求幫助,我可以修改我的代碼,以便搜索插入的路徑中的所有文件夾。

下面請看代碼:

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

回答

0

由於格式化你的代碼是非常難讀,但this page顯示你想要什麼,並使用Apache的共享庫做。

+0

謝謝我會看看這個網站。 – Noobling

相關問題