Skip to content

Conversation

@WEIZIBIN
Copy link

fix gzip byte array

correct "mock" string byte array is

new byte[]{31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -53, -51, 79, -50, 6, 0, 107, -4, 51, 63, 4, 0, 0, 0}

gzip code

public static void main(String[] args) throws Exception {
        String text = "mock";

        byte[] compressed = compress(text);

        System.out.println(toJavaByteArray(compressed));
    }

    public static byte[] compress(String str) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
            gzip.write(str.getBytes(StandardCharsets.UTF_8));
        }
        return bos.toByteArray();
    }

    public static String toJavaByteArray(byte[] data) {
        StringBuilder sb = new StringBuilder("new byte[]{");
        for (int i = 0; i < data.length; i++) {
            sb.append(data[i]);
            if (i < data.length - 1) {
                sb.append(", ");
            }
        }
        sb.append("};");
        return sb.toString();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant