2013-05-30 46 views
0

我目前有一個函數makeBackup(),它將整個目錄壓縮成一個zip文件,然而這些文件太大了,所以我們決定切換到LZMA。 我們發現了一個庫(lzma-java),但它似乎只壓縮一個文件,而我們使用的zip函數允許將文件和目錄添加到zip文件。使用java.util.zip轉換zip目錄功能使用LZMA

我們如何通過改變我們的功能來實現與LZMA相同的功能?我說我們的低於目前功能:

private static void makeBackup() 
    { 
     String backupPathString = "/home/backups"; 
     /* zip remote file */ 
     try 
     { 
      //name of zip file to create 
      String zipFilename = "backup.zip"; 

      //create ZipOutputStream object 
      ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFilename)); 

      //path to the currentFile to be zipped 
      File zipFolder = new File(backupPathString); 

      //get path prefix so that the zip file does not contain the whole path 
      // eg. if currentFile to be zipped is /home/lalit/test 
      // the zip file when opened will have test currentFile and not home/lalit/test currentFile 
      int len = zipFolder.getAbsolutePath().lastIndexOf(File.separator); 
      String baseName = zipFolder.getAbsolutePath().substring(0, len + 1) + File.separator + "todaybackups"; 

      zipFilesInPath(zipOutStream, backupPathString, baseName); 
      zipOutStream.flush(); 
      zipOutStream.close(); 
     } 
     catch (IOException e) 
     { 
     } 
    } 

    private static void zipFilesInPath(ZipOutputStream zipOutputStream, String filePath, String baseName) throws IOException 
    { 
     File currentFile = new File(filePath); 
     ArrayList<File> filesArrayList = new ArrayList<File>(Arrays.asList(currentFile.listFiles())); 
     if (filesArrayList.isEmpty()) 
     { 
      String name = currentFile.getAbsolutePath().substring(baseName.length()); 
      ZipEntry zipEntry = new ZipEntry(name + "/" + "."); 
      zipOutputStream.putNextEntry(zipEntry); 
     } 
     for (File file : filesArrayList) 
     { 
      if (file.isDirectory()) 
      { 
       zipFilesInPath(zipOutputStream, file.getAbsolutePath(), baseName); 
      } 
      else 
      { 
       String name = file.getAbsolutePath().substring(baseName.length()); 
       ZipEntry zipEntry = new ZipEntry(name); 
       zipOutputStream.putNextEntry(zipEntry); 
       IOUtils.copy(new FileInputStream(file), zipOutputStream); 
       zipOutputStream.closeEntry(); 
      } 
     } 
    } 

    private static void unzipFilesToPath(ZipInputStream zipInputStream, String fileExtractPath) throws IOException 
    { 
     ZipEntry entry; 
     while ((entry = zipInputStream.getNextEntry()) != null) 
     { 
      int count; 
      byte[] data = new byte[2048]; 

      /*let's make the directory structure needed*/ 
      File destFile = new File(fileExtractPath, entry.getName()); 
      File destinationParent = destFile.getParentFile(); 
      // create the parent directory structure if needed 
      destinationParent.mkdirs(); 

      if (!entry.isDirectory() && !entry.getName().substring(entry.getName().length() - 1).equals(".")) 
      { 
       final FileOutputStream fos = new FileOutputStream(fileExtractPath + File.separator + entry.getName()); 
       final BufferedOutputStream dest = new BufferedOutputStream(fos, 2048); 
       while ((count = zipInputStream.read(data, 0, 2048)) != -1) 
       { 
        dest.write(data, 0, count); 
       } 
       dest.flush(); 
       dest.close(); 
      } 
     } 
    } 
+0

如果你首先使用zip壓縮,然後lzma你有足夠好的壓縮率嗎? – fGo

+0

no ..如果我把同一個文件複製4次在一個目錄中,如果我用lzma直接壓縮它,我會得到1/4的大小,先做一個zip然後再做一次lzma壓縮 – dendini

回答