Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active June 17, 2025 12:59
Show Gist options
  • Save aspose-com-gists/b3fd8877f6da22615070b2f05901b706 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/b3fd8877f6da22615070b2f05901b706 to your computer and use it in GitHub Desktop.
UnZIP a GZ File in Java
package com.example;
import com.aspose.zip.GzipArchive;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class main {
public static void main(String[] args) {
// Set path for the working directories.
String dataDir = "data";
String outDir = "output";
// Initialize an object of the GzipArchive class to load the source gz file. s
try (GzipArchive archive = new GzipArchive(dataDir + "sample.gz")) {
// Extracts the archive and copies extracted content to file stream.
try (FileOutputStream extracted = new FileOutputStream(new File(outDir + "test.txt"))) {
InputStream unpacked = archive.open();
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = unpacked.read(buffer, 0, buffer.length)) > 0) {
extracted.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Successfully Opened GZip Archive");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment