0

Im downloading all gz format file from X site.After downloading all the files into temp folder when i exact with 0KB ..Pls help me out with code comments..Any changes in the code

 URL site = new URL (sb.toString());
         URLConnection uc = site.openConnection();             
        uc.setRequestProperty("Accept-Encoding", "gzip");//Here the change
        java.io.BufferedInputStream in = new BufferedInputStream(uc.getInputStream());
        //ZipInputStream gzin = new ZipInputStream(in);

         GZIPInputStream gzin = new GZIPInputStream(in);

         fileName = fileName.substring(fileName.lastIndexOf("/")+1);
         File destFile = new File("D:\\source\\"+fileName);
         java.io.FileOutputStream fos = new FileOutputStream(destFile);
         GZIPOutputStream gz = new GZIPOutputStream(fos);
         byte data[] = new byte[65536];
         int gsize = 0;
         while((gsize = gzin.read(data)) != -1)
         {
               gz.write(data, 0, gsize);
         }
Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • Have you closed your Fileoutputstream, i.e. fos ? – aryann Nov 28 '13 at 12:07
  • 1
    The files you are downloading are already gripped? Then you do not need to use the zipped streams. More importantly, it is imperative that you close both the input stream and the output stream when you are done as otherwise they will not flush. Also the zipped streams need to do some work at the end to finish the files. If you are using java 7 then use try-with-resources. – Boris the Spider Nov 28 '13 at 12:08
  • Further, `String` manipulation to get the name of a file is a big no-no. What if the last `/` is escaped? Use a `File` object and call the appropriate method - `File.getName()`. – Boris the Spider Nov 28 '13 at 12:10

1 Answers1

0

Add

gz.close();

at the end.

Also, there is no point in unzipping something if you are immediately going to zip it again. Remove both the GZipInputStream and the GZipOutputStream from your code if you really want to save the zipped version, because that is what you are currently doing.

Robin Green
  • 32,079
  • 16
  • 104
  • 187