5

I want to progammatically download a gzipped file and uncompress it, but instead of waiting for it to fully download before uncompressing it, I want to unzip it while its downloading, that is, unzip it on the fly. Is this even possible, or the the gzipped format prohibits such uncompression on the fly.

I am of course able to use the Java's GZIPInputStream library to uncompress a file part by part on a local file system, but in the local file system, I obviously have the full gzipped file. But is this possible when I don't have the full gzipped file beforehand, as in the case of downloading from the internet or cloud storage?

pythonic
  • 20,589
  • 43
  • 136
  • 219

1 Answers1

1

Since your URL connection is an inputstream, and since you create the gzipinputstream w/an inputstream, I think this is fairly 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();
}
Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28