2016-03-15 192 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

可能压缩最终以小块完成,这比一次压缩整个文件的效率低。 –