2015-10-14 18 views
0

我已經使用下面的Java代碼來解壓縮一個.GZ文件。 .gz文件包含一個文件夾/ textFile。只有在文本文件中有一些數據的情況下才能正常工作。即使文本文件中沒有數據,我也需要它工作。幫助我在下面的java代碼中進行任何修改。7-拉鍊JBinding使用解壓縮文件

public static void main(String[] args) { 
    String sourceZipFile = "C:/SKKIKRFULL20151014.gz"; 
    final String destinationDir = "C:/"; 
    RandomAccessFile randomAccessFile = null; 
    ISevenZipInArchive inArchive = null; 
    try { 
     randomAccessFile = new RandomAccessFile(sourceZipFile, "r"); 
     inArchive = SevenZip.openInArchive(null, // autodetect archive type 
       new RandomAccessFileInStream(randomAccessFile)); 

     // Getting simple interface of the archive inArchive 
     ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface(); 

     for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { 

      final int[] hash = new int[] { 0 }; 

      if (!item.isFolder()) { 

       ExtractOperationResult result; 
       result = item.extractSlow(new ISequentialOutStream() { 

        public int write(final byte[] data) throws SevenZipException { 
         try { 

          if (item.getPath().indexOf(File.separator) > 0) { 

           String path = destinationDir + File.separator 
             + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator)); 

           File folderExisting = new File(path); 

           if (!folderExisting.exists()) { 

            new File(path).mkdirs(); 
           } 
          } 
          OutputStream out = new FileOutputStream(
            destinationDir + File.separator + item.getPath()); 

          out.write(data); 
          out.close(); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
         hash[0] |= Arrays.hashCode(data); 
         return data.length; // Return amount of proceed data 
        } 
       }); 
       if (result == ExtractOperationResult.OK) { 
        System.out.println(String.format("%9X | %s", hash[0], item.getPath())); 
       } else { 
        System.err.println("Error extracting item: " + result); 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (inArchive != null) { 
      try { 
       inArchive.close(); 
      } catch (SevenZipException e) { 
       System.err.println("Error closing archive: " + e); 
       e.printStackTrace(); 
      } 
     } 
     if (randomAccessFile != null) { 
      try { 
       randomAccessFile.close(); 
      } catch (IOException e) { 
       System.err.println("Error closing file: " + e); 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

正如我調試它。它沒有進入下面的塊。

result = item.extractSlow(new ISequentialOutStream() {//write() method}) 

想知道如何修改代碼以使其工作。

+0

當文件沒有文本發生了什麼事?我假設「沒有文本」意味着它有一個0字節的長度。 – Marged

+0

是的。沒有文本表示它有0字節的長度,並且當沒有文本解壓縮沒有發生並且錯誤也沒有顯示。 – user2536077

回答

2

您的代碼只有當它的有效載荷比0字節的創建一個文件。我相信這是因爲ISequentialOutStream寫入(byte [] data) - 只有在數組大於0字節時才寫入。從javadocs:

寫入數據字節數組到流中。如果data.length> 0,則該函數必須至少寫入1個字節。

您應該更改write() - 方法代碼以允許創建空文件。 也許這會有所幫助:Stackoverflow: decompress files with .7z extension in java

+0

只有在文件中沒有文本時纔會進入寫入方法。我覺得它不進入下面只塊..結果= item.extractSlow(新ISequentialOutStream(){// write()方法}) – user2536077

+0

感謝您的代碼鏈接。但鏈接中提供的代碼只能用於我的代碼。如果文件中沒有數據,它不會解壓縮。 – user2536077