I have a problem with GZip in Java. Currently i work with files that are gzipped. One file in one gzip archive. And if i decompress them manually and then parse them everything works. But i want to automate this with Java and GZipInputStream but it doesn't work. I need to have DataInputStream at the end. My code is:
byte[] bytesArray = Files.readAllBytes(baseFile.toPath());
try {
reader = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(bytesArray)));
System.out.println("gzip");
} catch (ZipException notZip) {
reader = new DataInputStream(new ByteArrayInputStream(bytesArray));
System.out.println("no gzip");
}
I also tried new GZIPInputStream(new FileInputStream(baseFile)); The result is the same. Due to output i see that Gzip stream creates without exception but later i get invalid data from DataInputStream. Please help :)
I ran the following code without problems
public static void main(String[] args) throws IOException {
byte[] originalBytesArray = Files.readAllBytes(new File("OrdLog.BR-1.17.2016-09-12.bin").toPath());
byte[] bytesArray = Files.readAllBytes(new File("OrdLog.BR-1.17.2016-09-12.bin.gz").toPath());
DataInputStream reader = null;
try {
reader = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(bytesArray)));
System.out.println("gzip");
} catch (ZipException notZip) {
reader = new DataInputStream(new ByteArrayInputStream(bytesArray));
System.out.println("no gzip");
}
byte[] uncompressedBytesArray = new byte[originalBytesArray.length];
reader.readFully(uncompressedBytesArray);
reader.close();
boolean filesDiffer = false;
for (int i = 0; i < uncompressedBytesArray.length; i++) {
if (originalBytesArray[i] != uncompressedBytesArray[i]) {
filesDiffer = true;
}
}
System.out.println("Files differ: " + filesDiffer);
}
It reads the gzip file and the uncompressed file and compares the content. It prints Files differ: false. If it doesn't for your files than the files are not the same.