GZIPInputStream填充结束了吗?

时间:2014-07-31 07:36:28

标签: java junit gzip guava

我遇到了解压缩文件的奇怪问题,我正在考虑使用charset UTF-8。我正在使用Guava库。

public static byte[] gzip(final CharSequence cs, final Charset charset) throws IOException {
    final ByteArrayOutputStream os = new ByteArrayOutputStream(cs.length());
    final GZIPOutputStream gzipOs = new GZIPOutputStream(os);
    gzipOs.write(charset.encode(CharBuffer.wrap(cs)).array());
    Closeables.closeQuietly(gzipOs);
    return os.toByteArray();
}

public static boolean gzipToFile(final CharSequence from, final File to, final Charset charset) {
    try {
        Files.write(StreamUtils.gzip(from, charset), to);
        return true;
    } catch (final IOException e) {
        // ignore
    }
    return false;
}

public static String gunzipFromFile(final File from, final Charset charset) {
    String str = null;
    try {
        str = charset.decode(ByteBuffer.wrap(gunzip(Files.toByteArray(from)))).toString();
    } catch (final IOException e) {
        // ignore
    }
    return str;
}

public static byte[] gunzip(final byte[] b) throws IOException {
    GZIPInputStream gzipIs = null;
    final byte[] bytes;
    try {
        gzipIs = new GZIPInputStream(new ByteArrayInputStream(b));
        bytes = ByteStreams.toByteArray(gzipIs);
    } finally {
        Closeables.closeQuietly(gzipIs);
    }
    return bytes;
}

这里有一个小JUnit。为了测试我正在使用不同语言的lorem ipsum,如英语,德语,俄语......我首先将原始文本压缩为文件,然后解压缩文件并将其与原始文本进行比较:

@Test
public void gzip() throws IOException {
    final String originalText = Files.toString(ORIGINAL_IPSUM_LOREM, Charsets.UTF_8);

    // create temporary file
    final File tmpFile = this.tmpFolder.newFile("loremIpsum.txt.gz");

    // check if gzip write is OK
    final boolean status = StreamUtils.gzipToFile(originalText, tmpFile, Charsets.UTF_8);
    Assertions.assertThat(status).isTrue();
    Assertions.assertThat(Files.toByteArray(tmpFile)).isEqualTo(Files.toByteArray(GZIPPED_IPSUM_LOREM));

    // unzip it again
    final String uncompressedString = StreamUtils.gunzipFromFile(tmpFile, Charsets.UTF_8);
    Assertions.assertThat(uncompressedString).isEqualTo(originalText);
}

JUnit失败了以下: enter image description here

调试器显示uncompressedText和orignalText之间的区别:

[-17, -69, -65, 76, 111, ... (omitted) ... , -117, 32, -48, -66, -48, -76, -47, -128, 32, -48, -78, -48, -75, -47, -127, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... (omitted) ... , 0, 0, 0, 0]

..而且原始文本没有拖尾零:

[-17, -69, -65, 76, 111, ... (omitted) ... , -117, 32, -48, -66, -48, -76, -47, -128, 32, -48, -78, -48, -75, -47, -127, 46]

知道什么可能是错的???谢谢: - )

1 个答案:

答案 0 :(得分:5)

我认为问题在于:

    charset.encode(CharBuffer.wrap(cs)).array()

array()的{​​{3}}表示它返回ByteBuffer的后备数组。但支持数组可能比缓冲区的有效内容大......我怀疑在这种情况下它是。


FWIW ......我怀疑Buffer对象的明确用户和ByteArray流对象对性能的帮助很大。

我怀疑你最好这样做:

public static boolean gzipToFile(CharSequence from, File to, Charset charset) {
    try (FileOutputStream fos = new FileOutputStream(to);
         BufferedOutputStream bos = new BufferedOutputStream(fos);
         GZIPOutputStream gzos = new GZIPOutputStream(bos);
         OutputStreamWriter w = new OutputStreamWriter(gzos, charset)) {
        w.append(from);
        w.close();
        return true;
    } catch (final IOException e) {
        // ignore
    }
    return false;
}

(相当于阅读。)

为什么呢?我怀疑中间ByteArray流的额外副本很可能会抵消使用Buffer获得的潜在加速。

此外,我的直觉是,压缩/解压缩步骤将主导其他任何事情。