A memo that was tried and errored when creating the title process.
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.
test.java
byte[] bytes = output.toByteArray();
// byte[]Gzipped
String gzipStr = "";
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(bytes);
gzip.close();
gzipStr = out.toString();
out.close();
}catch (IOException e) {
e.printStackTrace();
return;
}
//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")))) {
writer.print(gzipStr);
} catch (IOException e) {
e.printStackTrace();
}
↑ 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".
If you check the file output by Stirlin, it is certainly not "1f8b" at the beginning. (But like "1fef" instead of "ef1f" ...)
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:
test.java
byte[] bytes = output.toByteArray();
String file = "Output file path";
try {
GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(file));
gzip.write(bytes);
gzip.close();
}catch (IOException e) {
e.printStackTrace();
return;
}
After outputting the file with this, it was able to be decompressed normally.
The binary file also starts with "1f8b", so it looks okay.
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