2016-03-15 189 views
3

我正在使用jpountz LZ4來嘗試壓縮文件,並且我想使用Java文件輸入和輸出流讀入和輸出文件。我試圖找到一個在線解決方案,但沒有什麼,我發現了一個關於如何正確實現LZ4的前一個stackoverflow問題,我已經採取了並試圖修改它以使用流,但我不確定這是否正確的或者它是否正在工作。使用輸入/輸出流的Java LZ4壓縮

當運行上的文本文件的壓縮它輸出具有某些字符丟失或與符號

ðHello world Heðo world Hello ðrld Hello worlðHello worl 

但具有圖像運行時,它的文件就引發出界錯誤的替換的文件。我也一直無法解壓縮,因爲它會拋出輸入緩衝區的錯誤解碼偏移量3。

這裏是我的代碼任何幫助,將不勝感激感謝

public void LZ4Compress(InputStream in, OutputStream out){ 
    int noBytesRead = 0;  //number of bytes read from input 
    int noBytesProcessed = 0; //number of bytes processed 
    try { 
     while ((noBytesRead = in.read(inputBuffer)) >= 0) { 
      noBytesProcessed = inputBuffer.length; 
      decompressedLength = inputBuffer.length; 
      outputBuffer = compress(inputBuffer, decompressedLength); 
      out.write(outputBuffer, 0, noBytesRead); 
     } 
     out.flush(); 
     in.close(); 
     out.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void LZ4decompress(InputStream in, OutputStream out){ 
    int noBytesRead = 0;  //number of bytes read from input 
    try { 
     while((noBytesRead = in.read(inputBuffer)) >= 0){ 
      noBytesProcessed = inputBuffer.length; 
      outputBuffer = decompress(inputBuffer); 
      out.write(outputBuffer, 0, noBytesRead); 

     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public static byte[] compress(byte[] src, int srcLen) { 
    decompressedLength = srcLen; 
    int maxCompressedLength = compressor.maxCompressedLength(decompressedLength); 
    byte[] compressed = new byte[maxCompressedLength]; 
    int compressLen = compressor.compress(src, 0, decompressedLength, compressed, 0, maxCompressedLength); 
    byte[] finalCompressedArray = Arrays.copyOf(compressed, compressLen); 
    return finalCompressedArray; 
} 

private static LZ4SafeDecompressor decompressor = factory.safeDecompressor(); 

public static byte[] decompress(byte[] finalCompressedArray) { 
    byte[] restored = new byte[finalCompressedArray.length]; 
    restored = decompressor.decompress(finalCompressedArray, finalCompressedArray.length); 
    return restored; 
} 
+0

@ user3758298-我有一個查詢就像如果我不知道解壓縮數組的長度,然後我應該如何解壓縮的字節數組?我有一個壓縮的字節數組。我想解壓縮它。 – kit

回答

1

所以我用LZ4block輸入解決我的問題/輸出流

public static void LZ4compress(String filename, String lz4file){ 
    byte[] buf = new byte[2048]; 
    try { 
     String outFilename = lz4file; 
     LZ4BlockOutputStream out = new LZ4BlockOutputStream(new FileOutputStream(outFilename), 32*1024*1024); 
     FileInputStream in = new FileInputStream(filename); 
     int len; 
     while((len = in.read(buf)) > 0){ 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
    } catch (IOException e) { 

    } 
} 

public static void LZ4Uncompress(String lz4file, String filename){ 
    byte[] buf = new byte[2048]; 
    try { 
     String outFilename = filename; 
     LZ4BlockInputStream in = new LZ4BlockInputStream(new FileInputStream(lz4file)); 
     FileOutputStream out = new FileOutputStream(outFilename); 
     int len; 
     while((len = in.read(buf)) > 0){ 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
    } catch (IOException e) { 

    } 
} 
1

只在代碼看,我會說你會錯在這裏:

outputBuffer = compress(inputBuffer, decompressedLength); 
out.write(outputBuffer, 0, noBytesRead); 

您已經在壓縮修剪OutputBuffer中。嘗試:

out.write(outputBuffer); 
+0

這就解決了我的越界錯誤,它似乎與所有文件類型一起運行,問題是輸出文件大於原始大小 – user3758298

+0

可能壓縮最終以小塊完成,這比一次壓縮整個文件的效率低。 –