`
border
  • 浏览: 201334 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

『Java 』zip文件下载

阅读更多

zip文件下载

    /**
     * 从srcUrlStr下载zip文件保存到descFilePath路径下
     * @param srcUrlStr
     * @param descFilePath
     * @param timeout
     * @return Boolean true:ok flase:Error
     */
    public static Boolean DownloadZip(String srcUrlStr, String descFilePath,
            int timeout) throws IOException {
       
        URL url = null;
        URLConnection urlcon = null;
        ZipInputStream zipis = null;
        ZipOutputStream zipos = null;

        try {
            url = new URL(srcUrlStr);
            urlcon = url.openConnection();
            int length = urlcon.getContentLength();
            urlcon.setReadTimeout (timeout);
            zipis = new ZipInputStream(urlcon.getInputStream());
            ZipEntry zipEntry = zipis.getNextEntry();

            File f = new File(descFilePath);
            zipos = new ZipOutputStream(new FileOutputStream(f));
            zipos.setMethod(ZipOutputStream.DEFLATED);
            zipos.putNextEntry(zipEntry);

            int len = (length > 100000) ? 100000 : length;
            byte[] bArray = new byte[len];
            int retVal = 0;
            while ((retVal = zipis.read(bArray, 0, len)) != -1) {
                zipos.write(bArray, 0, retVal);
            }
            zipis.close();
            zipos.flush ();
            zipos.close();
            return true;
        } catch (IOException ioe) {
            System.out.println("DownloadZip Err " + ioe.getMessage());
            return false;
        } finally {
            if (null != zipis) {
                zipis.close();
            }
            if (null != zipos) {
                zipos.close();
            }
        }
    }



--
Blog:     www.borderj.cn

  Border
分享到:
评论
1 楼 fanlei77 2009-08-25  
正好用到,谢谢哈

相关推荐

Global site tag (gtag.js) - Google Analytics