2013-05-30 3 views
0

현재 전체 디렉토리를 zip 파일로 압축하는 makeBackup() 함수가 있습니다. 파일이 너무 커서 LZMA로 전환하기로 결정했습니다. 이 작업을 수행하는 라이브러리 (lzma-java)가 하나의 파일 만 압축하는 것처럼 보였으 나 우리가 사용했던 zip 함수는 파일과 디렉토리를 zip 파일에 추가 할 수 있습니다.java.util.zip을 사용하여 LZMA를 사용하는 변환 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로 압축하면 처음에는 zip을 수행 한 후 1/4z 크기의 압축을 얻습니다. – dendini

답변