{"id":653,"date":"2021-07-08T13:06:57","date_gmt":"2021-07-08T05:06:57","guid":{"rendered":"https:\/\/qtvz.com\/?p=653"},"modified":"2023-01-28T15:58:32","modified_gmt":"2023-01-28T07:58:32","slug":"java%e5%ae%9e%e7%8e%b0%e5%af%b9zip%e5%92%8crar%e6%96%87%e4%bb%b6%e7%9a%84%e8%a7%a3%e5%8e%8b%e7%bc%a9","status":"publish","type":"post","link":"https:\/\/qtvz.com\/653.html","title":{"rendered":"Java\u5b9e\u73b0\u5bf9zip\u548crar\u6587\u4ef6\u7684\u89e3\u538b\u7f29"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><h5>\u901a\u8fc7java\u5b9e\u73b0\u5bf9zip\u548crar\u6587\u4ef6\u7684\u89e3\u538b\u7f29 <a href=\"https:\/\/qtvz.com\/redirect\/aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vemhhb3NxL3AvOTc3NzIxNi5odG1s\" target=\"_blank\">\u539f\u6587\u5730\u5740<\/a><\/h5>\n<h6>\u8bf4\u660e\uff1aRAR\u683c\u5f0f\u7684\u538b\u7f29\u5305\uff0c\u9700\u8981\u538b\u7f29\u5de5\u5177\u7248\u672c\u4e0d\u9ad8\u4e8e4.0\u7248\u672c\u5426\u5219\u4f1a\u89e3\u538b\u5931\u8d25<\/h6>\n<pre><code class=\"language-maven\">\/\/\u9700\u8981\u7684JAR\u5305\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.ant&lt;\/groupId&gt;\n    &lt;artifactId&gt;ant&lt;\/artifactId&gt;\n    &lt;version&gt;1.10.9&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.github.axet&lt;\/groupId&gt;\n    &lt;artifactId&gt;java-unrar&lt;\/artifactId&gt;\n    &lt;version&gt;1.7.0-8&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<pre><code class=\"language-java\">package com.xieyos.test.utils;\n\nimport de.innosystec.unrar.Archive;\nimport de.innosystec.unrar.NativeStorage;\nimport de.innosystec.unrar.rarfile.FileHeader;\nimport org.apache.tools.zip.ZipEntry;\nimport org.apache.tools.zip.ZipFile;\n\nimport java.io.*;\nimport java.util.Enumeration;\n\n\/**\n * @projectName:Test\n * @packageName:com.xieyos.base.com.xieyos.base\n * @className:ZipAndRarTools\n * @Description:[zip\u548crar\u89e3\u538b\u7f29\u5de5\u5177\u7c7b]\n * @Author: XieYang\n * @CreateDate: 2021-07-08 12:25\n * @blog:[\u65b9\u6cd5\u5177\u4f53\u4e0a\u884c\/\u4e0b\u884c\u53c2\u6570\u6587\u6863]\n *\/\npublic class ZipAndRarTools {\n    \/**\n     * \u89e3\u538brar\n     *\n     * @param sourceRarPath \u9700\u8981\u89e3\u538b\u7684rar\u6587\u4ef6\u5168\u8def\u5f84\n     * @param destDirPath   \u9700\u8981\u89e3\u538b\u5230\u7684\u6587\u4ef6\u76ee\u5f55\n     * @throws Exception\n     *\/\n    public static void unrar(String sourceRarPath, String destDirPath) throws Exception {\n        File sourceRar = new File(sourceRarPath);\n        File destDir = new File(destDirPath);\n        Archive archive = null;\n        FileOutputStream fos = null;\n        System.out.println(&quot;Starting \u5f00\u59cb\u89e3\u538b...&quot;);\n        try {\n            archive = new Archive(new NativeStorage(sourceRar));\n            FileHeader fh = archive.nextFileHeader();\n            int count = 0;\n            File destFileName = null;\n            while (fh != null) {\n                System.out.println((++count) + &quot;) &quot; + fh.getFileNameString());\n                String compressFileName = fh.getFileNameString().trim();\n                destFileName = new File(destDir.getAbsolutePath() + &quot;\/&quot; + compressFileName);\n                if (fh.isDirectory()) {\n                    if (!destFileName.exists()) {\n                        destFileName.mkdirs();\n                    }\n                    fh = archive.nextFileHeader();\n                    continue;\n                }\n                if (!destFileName.getParentFile().exists()) {\n                    destFileName.getParentFile().mkdirs();\n                }\n\n                fos = new FileOutputStream(destFileName);\n                archive.extractFile(fh, fos);\n                fos.close();\n                fos = null;\n                fh = archive.nextFileHeader();\n            }\n\n            archive.close();\n            archive = null;\n            System.out.println(&quot;Finished \u89e3\u538b\u5b8c\u6210!&quot;);\n        } catch (Exception e) {\n            throw e;\n        } finally {\n            if (fos != null) {\n                try {\n                    fos.close();\n                    fos = null;\n                } catch (Exception e) {\n                }\n            }\n            if (archive != null) {\n                try {\n                    archive.close();\n                    archive = null;\n                } catch (Exception e) {\n                }\n            }\n        }\n    }\n\n    \/**\n     * \u89e3\u538bZip\u6587\u4ef6\n     *\n     * @param zipFileName  \u9700\u8981\u89e3\u538b\u7f29\u7684\u6587\u4ef6\u4f4d\u7f6e\n     * @param descFileName \u5c06\u6587\u4ef6\u89e3\u538b\u5230\u67d0\u4e2a\u8def\u5f84\n     * @throws IOException\n     *\/\n    public static void unZip(String zipFileName, String descFileName) throws IOException {\n        System.out.println(&quot;\u6587\u4ef6\u89e3\u538b\u5f00\u59cb...&quot;);\n        String descFileNames = descFileName;\n        if (!descFileNames.endsWith(File.separator)) {\n            descFileNames = descFileNames + File.separator;\n        }\n        try {\n            ZipFile zipFile = new ZipFile(zipFileName);\n            ZipEntry entry = null;\n            String entryName = null;\n            String descFileDir = null;\n            byte[] buf = new byte[4096];\n            int readByte = 0;\n            @SuppressWarnings(&quot;rawtypes&quot;)\n            Enumeration enums = zipFile.getEntries();\n            while (enums.hasMoreElements()) {\n                entry = (ZipEntry) enums.nextElement();\n                entryName = entry.getName();\n                descFileDir = descFileNames + entryName;\n                if (entry.isDirectory()) {\n                    new File(descFileDir).mkdir();\n                    continue;\n                } else {\n                    new File(descFileDir).getParentFile().mkdir();\n                }\n                File file = new File(descFileDir);\n                OutputStream os = new FileOutputStream(file);\n                InputStream is = zipFile.getInputStream(entry);\n                while ((readByte = is.read(buf)) != -1) {\n                    os.write(buf, 0, readByte);\n                }\n                os.close();\n                is.close();\n            }\n            zipFile.close();\n            System.out.println(&quot;\u6587\u4ef6\u89e3\u538b\u6210\u529f!&quot;);\n        } catch (Exception e) {\n            System.out.println(&quot;\u6587\u4ef6\u89e3\u538b\u5931\u8d25!&quot;);\n            e.printStackTrace();\n        }\n\n    }\n\n    public static void main(String[] args) throws Exception {\n        \/\/ZipAndRarTools.unrar(newFile(&quot;D:\u5b58\u653e\u8d44\u6599\u7684\u538b\u7f29\u5305\u5458\u5de5\u6750\u6599.rar&quot;),newFile(&quot;D:\u5b58\u653e\u8d44\u6599\u7684\u975e\u538b\u7f29\u5305&quot;));\n\n        ZipAndRarTools.unZip(&quot;D:rarTestjar\u5305\u548c\u914d\u7f6e\u6587\u4ef6\u8d44\u6e90.zip&quot;, &quot;D:rarTest&quot;);\n        ZipAndRarTools.unrar(&quot;D:rarTestrar\u538b\u7f29\u5305.rar&quot;, &quot;D:rarTest&quot;);\n\n    }\n}<\/code><\/pre>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u901a\u8fc7java\u5b9e\u73b0\u5bf9zip\u548crar\u6587\u4ef6\u7684\u89e3\u538b\u7f29 \u539f\u6587\u5730\u5740 \u8bf4\u660e\uff1aRAR\u683c\u5f0f\u7684\u538b\u7f29\u5305\uff0c\u9700\u8981\u538b\u7f29\u5de5\u5177\u7248\u672c\u4e0d\u9ad8\u4e8e4.0\u7248\u672c\u5426\u5219\u4f1a\u89e3\u538b\u5931\u8d25 \/\/\u9700\u8981\u7684JAR\u5305 &lt;dependency&gt; &lt;gr \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[39],"tags":[],"views":2696,"_links":{"self":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/653"}],"collection":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/comments?post=653"}],"version-history":[{"count":1,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":654,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/653\/revisions\/654"}],"wp:attachment":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/media?parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/categories?post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/tags?post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}