Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用输入/输出流的Java LZ4压缩_Java_Lz4 - Fatal编程技术网

使用输入/输出流的Java LZ4压缩

使用输入/输出流的Java LZ4压缩,java,lz4,Java,Lz4,我正在使用jpountzlz4来尝试压缩文件,我想使用Java文件输入和输出流来读取和输出文件。我试着在网上找到一个解决方案,但什么都没有,我发现了一个关于如何正确实现LZ4的stackoverflow问题,我已经解决了这个问题,并试图修改它以使用流,但我不确定这是否正确,或者它是否起作用 在文本文件上运行压缩时,它会输出缺少某些字符或替换为符号的文件 ðHello world Heðo world Hello ðrld Hello worlðHello worl 但是当使用图像文件运行它时,

我正在使用jpountzlz4来尝试压缩文件,我想使用Java文件输入和输出流来读取和输出文件。我试着在网上找到一个解决方案,但什么都没有,我发现了一个关于如何正确实现LZ4的stackoverflow问题,我已经解决了这个问题,并试图修改它以使用流,但我不确定这是否正确,或者它是否起作用

在文本文件上运行压缩时,它会输出缺少某些字符或替换为符号的文件

ð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;
}

只看代码,我会说您在这里出了问题:

 outputBuffer = compress(inputBuffer, decompressedLength);
 out.write(outputBuffer, 0, noBytesRead);
您已经在compress中修剪了outputBuffer。尝试:

out.write(outputBuffer);

因此,我使用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) {

    }
}

这修复了我的越界错误,它似乎适用于所有文件类型,问题是输出文件比原始文件大。很可能压缩是以小块的形式结束的,这比一次压缩整个文件的效果要差。@user3758298-我有一个问题,比如如果我不知道解压缩数组的长度,那么我应该如何解压缩字节数组?我有一个压缩字节数组。我想把它解压缩。