---
title: "『Java 』zip文件下载"
author: "border"
date: "2007-09-06T19:14:57Z"
tags: []
slug: "java-zip"
draft: false
lastmod: "2007-09-06T19:14:57Z"
id: 121531
url: "https://border.iteye.com/blog/121531"
---

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 &gt; 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](http://www.borderj.cn)

  Border