简体   繁体   English

尝试在 Java 中使用 BEST_COMPRESSION 进行 GZIP 压缩

[英]trying to GZIP compressing using BEST_COMPRESSION in Java

class ZIP extends GZIPOutputStream {

    public ZIP(OutputStream out) throws IOException {
        super(out);
        def.setLevel(Deflater.BEST_COMPRESSION);
    }
}

public class Main {
    public static byte[] in(byte[] b) throws Exception {
        byte[] readBuffer = new byte[5000];
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        GZIPInputStream zipIn = new GZIPInputStream(in);
        b = Arrays.copyOf(readBuffer, zipIn.read(readBuffer, 0, readBuffer.length));
        in.close();
        zipIn.close();
        return b;
    }

    public static byte[] out(byte[] b) throws Exception {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        ZIP out = new ZIP(arrayOutputStream);
        new ObjectOutputStream(out).write(b);
        out.finish();
        arrayOutputStream.flush();
        return arrayOutputStream.toByteArray();
    }

    public static void main(String[] a) throws Exception {
        String message = "hello";
        System.out.println("orig: " + message.length());
        byte[] out = out(message.getBytes(StandardCharsets.UTF_8));
        byte[] in = in(out);
        System.out.println("decompressed: " + new String(in, StandardCharsets.UTF_8).length());
    }
}

Basically I want to compress the string before inserting into database基本上我想在插入数据库之前压缩字符串

I already know how to do it without BEST_COMPRESSION我已经知道如何在没有 BEST_COMPRESSION 的情况下做到这一点

but I want to use BEST_COMPRESSION for better compress但我想使用 BEST_COMPRESSION 进行更好的压缩

For example, I want to compress "hello" The result is:比如我要压缩"hello" 结果是:

orig: 5
decompressed: 4

I don't know which part is wrong.我不知道哪一部分是错的。

I'm not sure exactly what's going wrong in your code, though the use of ObjectOutputStream is suspicious.我不确定您的代码到底出了什么问题,尽管使用ObjectOutputStream是可疑的。 The following code seems to do what you want:以下代码似乎可以满足您的要求:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Main {

    public static byte[] compress(byte[] b) throws IOException {
        var out = new ByteArrayOutputStream();
        try (var gzipOut = new CustomGZIPOutputStream(out)) {
            gzipOut.write(b);
        }
        return out.toByteArray();
    }

    public static byte[] decompress(byte[] b) throws IOException {
        try (var gzipIn = new GZIPInputStream(new ByteArrayInputStream(b))) {
            return gzipIn.readAllBytes();
        }
    }

    public static void main(String[] a) throws IOException {
        String message = "Hello, World!";
        byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);

        System.out.println("Original message          : " + message);
        System.out.println("Original bytes length     : " + messageBytes.length);

        byte[] decompressedBytes = decompress(compress(messageBytes));
        String decompressedMessage = new String(decompressedBytes, StandardCharsets.UTF_8);

        System.out.println("Decompressed message      : " + decompressedMessage);
        System.out.println("Decompressed bytes length : " + decompressedBytes.length);
    }

    private static class CustomGZIPOutputStream extends GZIPOutputStream {

        CustomGZIPOutputStream(OutputStream out) throws IOException {
            super(out);
            def.setLevel(Deflater.BEST_COMPRESSION);
        }
    }
}

Output: Output:

Original message          : Hello, World!
Original bytes length     : 13
Decompressed message      : Hello, World!
Decompressed bytes length : 13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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