Can I unzip a gzip file when I download it?

I want to progammatically download the gzip file and unzip it, but instead of waiting for it to fully download before unpacking, I want to unzip it while downloading it, that is, unzip it on the fly. Is this possible, or does the gzipped format prohibit such compression on the fly.

Of course, I can use the GZIPInputStream Java library to unzip part of the file in parts on the local file system, but on the local file system I obviously have the full gzipped file. But is this possible when I do not have the full gzipped file, as is the case with downloading from the Internet or cloud storage?

+4
source share
1 answer

Since your URL connection is an input stream, and since you are creating a gzipinputstream with an input stream, I think this is pretty straight forward?

public static void main(String[] args) throws Exception {
    URL someUrl = new URL("http://your.site.com/yourfile.gz");
    HttpURLConnection someConnection = (HttpUrlConnection) someUrl.openConnection();
    GZIPInputStream someStream = new GZIPInputStream(someConnection.getInputStream());
    FileOutputStream someOutputStream = new FileOutputStream("output.tar");
    byte[] results = new byte[1024];
    int count = someStream.read(results);
    while (count != -1) {
        byte[] result = Arrays.copyOf(results, count);
        someOutputStream.write(result);
        count = someStream.read(results);
    }
    someOutputStream.flush();
    someOutputStream.close();
    someStream.close();
}
+1
source

Source: https://habr.com/ru/post/1681479/


All Articles