简体   繁体   English

如何使用GZIPINPUT / GZIPOUTPUT流将.gz文件下载到temp文件夹

[英]how to download .gz files to the temp folder using GZIPINPUT/GZIPOUTPUT stream

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 我从X站点下载了所有gz格式的文件。将所有文件下载到temp文件夹中时,精确到0KB ..请帮助我进行代码注释..代码中的任何更改

 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);
         }

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. 如果您确实要保存压缩版本,请从代码中删除GZipInputStream和GZipOutputStream,因为这是您当前正在执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM