2013-05-05 173 views
0

我的任務需要我將文件目錄保存到zip文件夾中。我唯一的問題是我需要保留子目錄作爲主目錄中的文件夾。文件系統看起來像在Java中以Zip文件夾創建文件夾

C\\Friends 
C:\\Friends\\Person1\\Information.txt 
C:\\Friends\\Person2\\Information.txt 
C:\\Friends\\Person3\\Information.txt 

。 。 。

現在我可以寫我的zip文件夾裏面的txt文件,但是在我的zip文件夾中我需要保留那個文件夾結構。我知道我的代碼現在的方式會告訴我,我試圖寫的文件是關閉的(無法訪問)。我的功能到目前爲止:

private String userDirectroy = "" //This is set earlier in the program 

public void exportFriends(String pathToFile) 
    { 
     String source = pathToFile + ".zip"; 

     try 
     { 


      String sourceDir = userDirectory; 
      String zipFile = source; 

      try 
      { 

        FileOutputStream fout = new FileOutputStream(zipFile); 


        ZipOutputStream zout = new ZipOutputStream(fout); 


        File fileSource = new File(sourceDir); 

        addDirectory(zout, fileSource); 


        zout.close(); 

        System.out.println("Zip file has been created!"); 
      }  
      catch(Exception e) 
      { 

      } 

     } 
     catch(Exception e) 
     { 
      System.err.println("First Function: " + e); 

     } 
    } 

    private static void addDirectory(ZipOutputStream zout, File fileSource) { 


     File[] files = fileSource.listFiles(); 

     System.out.println("Adding directory " + fileSource.getName()); 

     for(int i=0; i < files.length; i++) 
     { 

       if(files[i].isDirectory()) 
       { 
        try 
        { 
         byte[] buffer = new byte[1024]; 
         FileInputStream fin = new FileInputStream(files[i]); 
         zout.putNextEntry(new ZipEntry(files[i].getName())); 

         int length; 

         while((length = fin.read(buffer)) > 0) 
         { 
          zout.write(buffer, 0, length); 
         } 
        } 
        catch(Exception e) 
        { 
         System.err.println(e); 
        } 
         addDirectory(zout, files[i]); 
         continue; 
       } 


       try 
       { 
         System.out.println("Adding file " + files[i].getName()); 

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

         //create object of FileInputStream 
         FileInputStream fin = new FileInputStream(files[i]); 

         zout.putNextEntry(new ZipEntry(files[i].getName())); 


         int length; 

         while((length = fin.read(buffer)) > 0) 
         { 
          zout.write(buffer, 0, length); 
         } 



          zout.closeEntry(); 

          //close the InputStream 
          fin.close(); 

       } 
       catch(IOException ioe) 
       { 
         System.out.println("IOException :" + ioe);        
       } 
     } 

} 

任何幫助將不勝感激。謝謝

回答

1

對於每個文件夾,您需要添加一個空的ZipEntry路徑。

對於每個文件,您需要提供路徑和文件名。這需要你知道剝掉路徑的一部分,這將是啓動目錄

擴展概念

所以之後的一切,從你的榜樣,如果起始目錄是C:\Friends,那麼對於C:\Friends\Person1\Information.txt條目應該看起來像Person1\Information.txt

public void exportFriends(String pathToFile) { 

    String source = pathToFile + ".zip"; 
    try { 
     String sourceDir = "C:/Friends"; 
     String zipFile = source; 

     try { 
      FileOutputStream fout = new FileOutputStream(zipFile); 
      ZipOutputStream zout = new ZipOutputStream(fout); 

      File fileSource = new File(sourceDir); 

      addDirectory(zout, sourceDir, fileSource); 

      zout.close(); 

      System.out.println("Zip file has been created!"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static String getRelativePath(String sourceDir, File file) { 
    // Trim off the start of source dir path... 
    String path = file.getPath().substring(sourceDir.length()); 
    if (path.startsWith(File.pathSeparator)) { 
     path = path.substring(1); 
    } 
    return path; 
} 

private static void addDirectory(ZipOutputStream zout, String sourceDir, File fileSource) throws IOException { 
    if (fileSource.isDirectory()) { 
     // Add the directory to the zip entry... 
     String path = getRelativePath(sourceDir, fileSource); 
     if (path.trim().length() > 0) { 
      ZipEntry ze = new ZipEntry(getRelativePath(sourceDir, fileSource)); 
      zout.putNextEntry(ze); 
      zout.closeEntry(); 
     } 

     File[] files = fileSource.listFiles(); 
     System.out.println("Adding directory " + fileSource.getName()); 
     for (int i = 0; i < files.length; i++) { 
      if (files[i].isDirectory()) { 
       addDirectory(zout, sourceDir, files[i]); 
      } else { 

       System.out.println("Adding file " + files[i].getName()); 

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

       //create object of FileInputStream 
       FileInputStream fin = new FileInputStream(files[i]); 
       zout.putNextEntry(new ZipEntry(getRelativePath(sourceDir, files[i]))); 

       int length; 

       while ((length = fin.read(buffer)) > 0) { 
        zout.write(buffer, 0, length); 
       } 
       zout.closeEntry(); 
       //close the InputStream 
       fin.close(); 
      } 
     } 
    } 
} 
+0

他的代碼運行正常,但我無法打開文件? – 2013-11-15 10:20:54