Gzip-compress byte array in Java and output to file

A memo that was tried and errored when creating the title process.

The source I wrote for the time being

I made the byte array gzip and output it to a file like this. When I modified the process of outputting a file for another purpose earlier, I added it while checking this and that, and now I think about it, it is a mysterious and redundant way of writing.


        byte[] bytes = output.toByteArray();
        // byte[]Gzipped
        String gzipStr = "";
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzipStr = out.toString();
        }catch (IOException e) {
    	//Export gzipped data to a file
        String file = "Output file path";
        try(PrintWriter writer = new PrintWriter(
            new BufferedWriter(
            new OutputStreamWriter(
            new FileOutputStream
              (file),"UTF-8")))) {
        } catch (IOException e) {

error contents

↑ When I compile and execute Java and try to decompress the output file, the following error occurs.

01-05 08:35:22.294 6117-6117/? W/System.err: java.io.IOException: unknown format (magic number ef1f)
01-05 08:35:22.294 6117-6117/? W/System.err:     at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:109)
01-05 08:35:22.294 6117-6117/? W/System.err:     at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:88)
The following is omitted

I'm ashamed to say that I don't understand anything, and I googled "What is a gzip magic number?" (Magic number (format identifier)-wikipedia)

In gzip, the beginning of the file starts with "1f8b", which is the magic number (code for judging the format), but it seems that the file format cannot be judged because this file starts with "ef1f".

Check the binary file

If you check the file output by Stirlin, it is certainly not "1f8b" at the beginning. (But like "1fef" instead of "ef1f" ...) 無題.png

Source fix

I found the following site after investigating whether it was bad to knead it at the time of output. Compress and decompress GZIP files with ANDROID --TECHBOOSTER

Well, I was surprised that it was okay to write it so simply, and here is a modified source:


        byte[] bytes = output.toByteArray();
        String file = "Output file path";
        try {
            GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(file));
        }catch (IOException e) {

After outputting the file with this, it was able to be decompressed normally.

The binary file also starts with "1f8b", so it looks okay. 無題.png

For some reason, I wrote it with a text editor without using an IDE, but I was completely disappointed with just importing ... Java should use an IDE ...

Recommended Posts

Gzip-compress byte array in Java and output to file
Log output to file in Java
How to convert a file to a byte array in Java
How to ZIP a JAVA CSV file and manage it in a Byte array
Convert a Java byte array to a string in hexadecimal notation
[Java] How to output and write files!
Sample to read and write LibreOffice Calc fods file in Java 2021
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Enable log output to both file and console using log4j in Eclipse.
The story of forgetting to close a file in Java and failing
How to record JFR (Java Flight Recorder) and output a dump file
[Java] How to get and output standard input
I tried to output multiplication table in Java
How to input / output IBM mainframe files in Java?
Convert the array of errors.full_messages to characters and output
Java classes and instances to understand in the figure
How to convert A to a and a to A using AND and OR in Java
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
Output javadoc to word file
[Java] Convert ArrayList to array
How to initialize Java array
How to read your own YAML file (*****. Yml) in Java
Generate and execute Jar file of Java file belonging to package
[Java] I want to convert a byte array to a hexadecimal number
Read WAV data as a byte array in Android Java
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
Reasons to use Servlet and JSP separately in Java development
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
How to develop and register a Sota app in Java
Get attributes and values from an XML file in Java
Differences in how to handle strings between Java and Perl
Read Java properties file in C #
Multithreaded to fit in [Java] template
Encoding and Decoding example in Java
How to learn JAVA in 7 days
Unzip the zip file in Java
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
How to output the value when there is an array in the array
[Java] Declare and initialize an array
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Mixed Western calendar output in Java
Output PDF and TIFF with Java 8
[Java] Shallow copy and deep copy when converting an array to List
Cast an array of Strings to a List of Integers in Java
Read the first 4 bytes of the Java class file and output CAFEBABE
[Java] Difference between array and ArrayList
Understanding equals and hashCode in Java
Sample to read and write LibreOffice Calc fods file in JRuby 2021
How to use classes in Java?
How to name variables in Java
About file copy processing in Java
[Android] Convert Map to JSON using GSON in Kotlin and Java
Try to implement Yubaba in Java
[Java] Conversion from array to List
How to make a Java array
[Java] Convert array to ArrayList * Caution
How to encrypt and decrypt with RSA public key in Java
How to get the length of an audio file in java
Hello world in Java and Gradle