2014-12-23 28 views
1

這是我對正在壓縮文件夾代碼:JAVA:對正在壓縮文件夾的問題

List<String> filesListInDir = new ArrayList<String>(); 
public void populateFilesList(File dir) throws IOException { 
    File[] files = dir.listFiles(); 
    for(File file : files){ 
     if(file.isFile()) filesListInDir.add(file.getAbsolutePath()); 
     else populateFilesList(file); 
    } 
} 
public void zipDirectory(File dir, String zipDirName) { 
    try { 
     populateFilesList(dir); 
     //now zip files one by one 
     //create ZipOutputStream to write to the zip file 
     FileOutputStream fos = new FileOutputStream(zipDirName); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     for(String filePath : filesListInDir){ 
      System.out.println("Zipping "+filePath); 
      //for ZipEntry we need to keep only relative file path, so we used substring on absolute path 
      ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length()+1, filePath.length())); 
      zos.putNextEntry(ze); 
      //read the file and write to ZipOutputStream 
      FileInputStream fis = new FileInputStream(filePath); 
      byte[] buffer = new byte[1024]; 
      int len; 
      while ((len = fis.read(buffer)) > 0) { 
       zos.write(buffer, 0, len); 
      } 
      zos.closeEntry(); 
      fis.close(); 
     } 
     zos.close(); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我的問題是我有一個目錄(Main_Folder)2個文件夾(FOLDER_1和Folder_2)。當壓縮Folder_1時,壓縮文件包含Folder_2。如何刪除我的Folder_1.zip上的Folder_2?

這裏是我的觀點

  • Main_Folder

    • FOLDER_1
      • asd.sql
      • asd2.sql
      • asd3.sql
    • Folder_2
      • asd.jar

當正在壓縮FOLDER_1它由

  • Folder_1.zip
    • h //此文件夾包含Folder_2上的文件。所以,我需要刪除該文件夾
    • asd.sql
    • asd2.sql
    • asd3.sql

是否有任何方法或想法,我刪除這個 「H」 文件夾壓縮文件夾?

回答

0

我懷疑,因爲你正在使用instace變量filesListInDir荏苒您以前在加載的文件 ,更明確的是,一個在zipDirectory末之前來存儲文件,我你是不是在一個多采用這種假設線程化的環境,因爲它會因爲狀態變量而出現奇怪的情況。

public void zipDirectory(File dir, String zipDirName) { 
    try { 
     populateFilesList(dir); 
     //now zip files one by one 
     //create ZipOutputStream to write to the zip file 
     FileOutputStream fos = new FileOutputStream(zipDirName); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     for(String filePath : filesListInDir){ 
      System.out.println("Zipping "+filePath); 
      //for ZipEntry we need to keep only relative file path, so we used substring on absolute path 
      ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length()+1, filePath.length())); 
      zos.putNextEntry(ze); 
      //read the file and write to ZipOutputStream 
      FileInputStream fis = new FileInputStream(filePath); 
      byte[] buffer = new byte[1024]; 
      int len; 
      while ((len = fis.read(buffer)) > 0) { 
       zos.write(buffer, 0, len); 
      } 
      zos.closeEntry(); 
      fis.close(); 
     } 
     zos.close(); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     filesListInDir.clear(); // Clear the files 
    } 
}