{"id":272,"date":"2016-03-11T16:51:54","date_gmt":"2016-03-11T08:51:54","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=272"},"modified":"2016-03-11T16:51:54","modified_gmt":"2016-03-11T08:51:54","slug":"%e4%bd%bf%e7%94%a8java%e5%af%b9%e6%96%87%e4%bb%b6%e5%a4%b9%e8%bf%9b%e8%a1%8czip%e8%a7%a3%e5%8e%8b%e7%bc%a9","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=272","title":{"rendered":"\u4f7f\u7528java\u5bf9\u6587\u4ef6\u5939\u8fdb\u884czip\u89e3\u538b\u7f29"},"content":{"rendered":"<p>\n\t\u770b\u4e86\u51e0\u7bc7zip\u538b\u7f29\u7b97\u6cd5\u7684\u5e16\u5b50\uff0c\u8bb2\u7684\u7b97\u662f\u6bd4\u8f83\u7ec6\u81f4\u4e86\u3002\u4f46\u662f\u7b97\u6cd5\u53ea\u662f\u8bb2\u7b97\u6cd5\uff0c\u6ca1\u6709\u6700\u7ec8\u7684\u5b9e\u4f8b\u4ee3\u7801\u3002<br \/>\n\t\u4eca\u5929\u6211\u5c31\u4f7f\u7528java\u81ea\u5e26\u7684java.util.zip.*\u6765\u5b9e\u73b0zip\u7684\u89e3\u538b\u7f29\u3002<br \/>\n\t\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre>\r\npublic class ZipUtil {\r\n  \r\n    \/** \r\n     * \u6dfb\u52a0\u5230\u538b\u7f29\u6587\u4ef6\u4e2d \r\n     * @param out \r\n     * @param f \r\n     * @param base \r\n     * @throws Exception \r\n     *\/  \r\n    private void directoryZip(ZipOutputStream out, File f, String base) throws Exception {  \r\n        \/\/ \u5982\u679c\u4f20\u5165\u7684\u662f\u76ee\u5f55  \r\n        if (f.isDirectory()) {  \r\n            File[] fl = f.listFiles();  \r\n            \/\/ \u521b\u5efa\u538b\u7f29\u7684\u5b50\u76ee\u5f55  \r\n            out.putNextEntry(new ZipEntry(base + &quot;\/&quot;));  \r\n            if (base.length() == 0) {  \r\n                base = &quot;&quot;;  \r\n            } else {  \r\n                base = base + &quot;\/&quot;;  \r\n            }  \r\n            for (int i = 0; i &lt; fl.length; i++) {  \r\n                directoryZip(out, fl[i], base + fl[i].getName());  \r\n            }  \r\n        } else {  \r\n            \/\/ \u628a\u538b\u7f29\u6587\u4ef6\u52a0\u5165rar\u4e2d  \r\n            out.putNextEntry(new ZipEntry(base));  \r\n            FileInputStream in = new FileInputStream(f);  \r\n            byte[] bb = new byte[10240];  \r\n            int aa = 0;  \r\n            while ((aa = in.read(bb)) != -1) {  \r\n                out.write(bb, 0, aa);  \r\n            }  \r\n            in.close();  \r\n        }  \r\n    }  \r\n  \r\n    \/** \r\n     * \u538b\u7f29\u6587\u4ef6 \r\n     * @param zos \r\n     * @param file \r\n     * @throws Exception \r\n     *\/  \r\n    private void fileZip(ZipOutputStream zos, File file) throws Exception {  \r\n        if (file.isFile()) {  \r\n            zos.putNextEntry(new ZipEntry(file.getName()));  \r\n            FileInputStream fis = new FileInputStream(file);  \r\n            byte[] bb = new byte[10240];  \r\n            int aa = 0;  \r\n            while ((aa = fis.read(bb)) != -1) {  \r\n                zos.write(bb, 0, aa);  \r\n            }  \r\n            fis.close();  \r\n            System.out.println(file.getName());  \r\n        } else {  \r\n            directoryZip(zos, file, &quot;&quot;);  \r\n        }  \r\n    }  \r\n  \r\n    \/** \r\n     * \u89e3\u538b\u7f29\u6587\u4ef6 \r\n     * @param zis \r\n     * @param file \r\n     * @throws Exception \r\n     *\/  \r\n    private void fileUnZip(ZipInputStream zis, File file) throws Exception {  \r\n        ZipEntry zip = zis.getNextEntry();  \r\n        if (zip == null)  \r\n            return;  \r\n        String name = zip.getName();  \r\n        File f = new File(file.getAbsolutePath() + &quot;\/&quot; + name);  \r\n        if (zip.isDirectory()) {  \r\n            f.mkdirs();  \r\n            fileUnZip(zis, file);  \r\n        } else {  \r\n            f.createNewFile();  \r\n            FileOutputStream fos = new FileOutputStream(f);  \r\n            byte b[] = new byte[10240];  \r\n            int aa = 0;  \r\n            while ((aa = zis.read(b)) != -1) {  \r\n                fos.write(b, 0, aa);  \r\n            }  \r\n            fos.close();  \r\n            fileUnZip(zis, file);  \r\n        }  \r\n    }  \r\n      \r\n    \/** \r\n     * \u6839\u636efilePath\u521b\u5efa\u76f8\u5e94\u7684\u76ee\u5f55 \r\n     * @param filePath \r\n     * @return \r\n     * @throws IOException \r\n     *\/  \r\n    private File mkdirFiles(String filePath) throws IOException{  \r\n        File file = new File(filePath);  \r\n        if(!file.getParentFile().exists()){  \r\n            file.getParentFile().mkdirs();  \r\n        }  \r\n        file.createNewFile();  \r\n        return file;  \r\n    }  \r\n  \r\n    \/** \r\n     * \u5bf9zipBeforeFile\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u538b\u7f29\uff0c\u4fdd\u5b58\u4e3a\u6307\u5b9a\u7684\u6587\u4ef6zipAfterFile \r\n     * @param zipBeforeFile     \u538b\u7f29\u4e4b\u524d\u7684\u6587\u4ef6 \r\n     * @param zipAfterFile      \u538b\u7f29\u4e4b\u540e\u7684\u6587\u4ef6 \r\n     *\/  \r\n    public void zip(String zipBeforeFile, String zipAfterFile) {  \r\n        try {  \r\n            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(mkdirFiles(zipAfterFile)));  \r\n            fileZip(zos, new File(zipBeforeFile));  \r\n            zos.close();  \r\n        } catch (Exception e) {  \r\n            e.printStackTrace();  \r\n        }  \r\n    }  \r\n  \r\n    \/** \r\n     * \u89e3\u538b\u7f29\u6587\u4ef6unZipBeforeFile\u4fdd\u5b58\u5728unZipAfterFile\u76ee\u5f55\u4e0b \r\n     * @param unZipBeforeFile       \u89e3\u538b\u4e4b\u524d\u7684\u6587\u4ef6 \r\n     * @param unZipAfterFile        \u89e3\u538b\u4e4b\u540e\u7684\u6587\u4ef6 \r\n     *\/  \r\n    public void unZip(String unZipBeforeFile, String unZipAfterFile) {  \r\n        try {  \r\n            ZipInputStream zis = new ZipInputStream(new FileInputStream(unZipBeforeFile));  \r\n            File f = new File(unZipAfterFile);  \r\n            f.mkdirs();  \r\n            fileUnZip(zis, f);  \r\n            zis.close();  \r\n        } catch (Exception e) {  \r\n            e.printStackTrace();  \r\n        }  \r\n    }  \r\n    \r\n    public static void main(String[] args) {\r\n    \tZipUtil zipUtil = new ZipUtil();\r\n    \tzipUtil.zip(&quot;C:\\\\Users\\\\herman.xiong\\\\Desktop\\\\taoge.html&quot;, &quot;C:\\\\Users\\\\herman.xiong\\\\Desktop\\\\\u4e1a\u4f59\u8349.zip&quot;);\r\n    \tzipUtil.unZip(&quot;C:\\\\Users\\\\herman.xiong\\\\Desktop\\\\\u4e1a\u4f59\u8349.zip&quot;, &quot;C:\\\\Users\\\\herman.xiong\\\\Desktop\\\\\u4e1a\u4f59\u8349&quot;);\r\n    }\r\n}\r\n<\/pre>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=272\">\u4f7f\u7528java\u5bf9\u6587\u4ef6\u5939\u8fdb\u884czip\u89e3\u538b\u7f29<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u770b\u4e86\u51e0\u7bc7zip\u538b\u7f29\u7b97\u6cd5\u7684\u5e16\u5b50\uff0c\u8bb2\u7684\u7b97\u662f\u6bd4\u8f83\u7ec6\u81f4\u4e86\u3002\u4f46\u662f\u7b97\u6cd5\u53ea\u662f\u8bb2\u7b97\u6cd5\uff0c\u6ca1\u6709\u6700\u7ec8\u7684\u5b9e\u4f8b\u4ee3\u7801\u3002 \u4eca\u5929\u6211\u5c31\u4f7f\u7528jav [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":273,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,74],"class_list":["post-272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-zip"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=272"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions\/274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/media\/273"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}