简体   繁体   中英

Gzip used for compression returns the similar size of that of original

This code I have written for compressing the inputstream

public InputStream compress(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        byte[] inBytes = IOUtils.toByteArray(inputStream);
        gzipOutputStream.write(inBytes);
        ByteArrayInputStream retStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        gzipOutputStream.close();
        return retStream;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

It is returning the similar size's stream. Is their any bug in the code? The input stream passed is of an image file through a test as :

@Test
public void test() throws IOException {
    try {
        in = this.getClass().getResourceAsStream("/testimage.jpg");
    } catch (NullPointerException e) {
        logger.log(Level.SEVERE, "Exception reading file in test - ", e);
    }
    compress(in);
}

A JPEG (.jpg) file is already compressed . Trying to compress it again will most likely expand it a tiny bit.

If you compress already compressed content it wont be that much smaller (can be even bigger). Infinite compression is not possible. JPEG is already comressed (loosely). If you would GZIP bitmap or simple text file, you would notice the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM