{"id":5700,"date":"2024-09-05T08:01:01","date_gmt":"2024-09-05T00:01:01","guid":{"rendered":""},"modified":"2024-09-05T08:01:01","modified_gmt":"2024-09-05T00:01:01","slug":"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5700.html","title":{"rendered":"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d"},"content":{"rendered":"

\u6700\u8fd1\u7531\u4e8e\u5ba2\u6237\u6765\u56de\u4fee\u6539\u9700\u6c42\uff0c\u4f7f\u5f97\u6211\u5bf9\u4ee3\u7801\u4e5f\u662f\u53cd\u590d\u4fee\u6539\uff0c\u6700\u7ec8\u6298\u817e\u5b8c\u6bd5\u540e\uff0c\u8ba9\u6211\u603b\u7ed3\u4e863\u79cdJava\u751f\u6210\u7684zip\u5305\u3002<\/p>\n

1.\u666e\u901azip\u5305\uff0c\u89e3\u538b\u540e\u7684\u6837\u5b50\u4e0e\u6253\u5305\u538b\u7f29\u524d\u4e00\u6a21\u4e00\u6837\uff0c\u76f4\u63a5\u89e3\u538b\u5373\u53ef\u3002<\/h3>\n

\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n

\u6253\u5305E\u76d8\u4e0bpacks\u6587\u4ef6\u5939\u4e2d\u7684\u5185\u5bb9\uff0c\u751f\u6210\u7684zip\u5305\u8f93\u51fa\u5230E\u76d8\u4e0b\uff0c\u540d\u79f0\u4e3apacks.zip<\/p>\n

\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\u8fc7\u7a0b\u6ce8\u91ca\u5373\u53ef\u3002<\/p>\n

import java.nio.file.Paths; \/\/compress\u65b9\u6cd5\u9700\u8981\u4f20\u51652\u4e2a\u53c2\u6570\uff0c\u662f\u4e24\u4e2a\u5730\u5740 \/\/\u7b2c\u4e00\u4e2a\u5730\u5740\u662f\u76ee\u6807\u6253\u5305\u6587\u4ef6\u7684\u5730\u5740\uff0c\u7b2c\u4e8c\u4e2a\u662fzip\u5305\u8f93\u51fa\u7684\u5730\u5740 public static void main(String[] args) throws IOException { String hallFilePath = \"E:\/\" + \"packs\"; compress(Paths.get(hallFilePath).toString(), hallFilePath + \".zip\"); } \/\/\u7531\u6b64\u5f00\u59cb\u662f\u6240\u6709\u76f8\u5173\u7684\u5de5\u5177\u65b9\u6cd5 public static void compress(String fromPath, String toPath) throws IOException { File fromFile = new File(fromPath); File toFile = new File(toPath); if (!fromFile.exists()) { throw new ServiceException(fromPath + \"\u4e0d\u5b58\u5728\uff01\"); } try (FileOutputStream outputStream = new FileOutputStream(toFile); CheckedOutputStream checkedOutputStream = new CheckedOutputStream(outputStream, new CRC32()); ZipOutputStream zipOutputStream = new ZipOutputStream(checkedOutputStream)) { String baseDir = \"\"; compress(fromFile, zipOutputStream, baseDir); } } private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException { if (file.isDirectory()) { compressDirectory(file, zipOut, baseDir); } else { compressFile(file, zipOut, baseDir); } } private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException { if (!file.exists()) { return; } try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { ZipEntry entry = new ZipEntry(baseDir + file.getName()); zipOut.putNextEntry(entry); int count; byte[] data = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipOut.write(data, 0, count); } } } private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException { File[] files = dir.listFiles(); if (files != null && ArrayUtils.isNotEmpty(files)) { for (File file : files) { compress(file, zipOut, baseDir + dir.getName() + File.separator); } } }<\/code><\/pre>\n

\u538b\u7f29\u524d\u7684\u76ee\u6807\u6587\u4ef6\u5982\u4e0b\uff1a<\/h4>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n

 \u538b\u7f29\u540e\u8f93\u51fa\u7684\u6587\u4ef6\u4ee5\u53ca\u89e3\u538b\u540e\uff1a<\/h4>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n

2.\u538b\u7f29\u540e\u7684\u538b\u7f29\u5305\u89e3\u538b\u5373\u662f\u6587\u4ef6\uff0c\u4e0d\u9700\u8981\u6587\u4ef6\u5939\uff0c\u5c31\u662f\u4e0a\u9762\u7684\u6848\u4f8b\uff0c\u89e3\u538b\u540e\u8fd8\u6709\u4e00\u4e2apacks\u6587\u4ef6\u5939\uff0c\u8fd9\u91cc\u8df3\u8fc7\uff0c\u89e3\u538b\u51fa\u6765\u5c31\u662f\u6587\u4ef6\u5217\u8868\uff1a<\/h3>\n

\u5927\u4f53\u6b65\u9aa4\u76f8\u4f3c\uff0c\u6253\u5305\u65f6\u4ee3\u7801\u9700\u8981\u8c03\u6574\uff0c\u5148\u4e0a\u5168\u90e8\u7684\u4ee3\u7801\uff1a<\/p>\n

public static void main(String[] args) throws IOException { String hallFilePath = \"E:\/\" + \"packs\"; compress(Paths.get(hallFilePath).toString(), hallFilePath + \".zip\"); } public static void compress(String fromPath, String toPath) throws IOException { File fromFile = new File(fromPath); File toFile = new File(toPath); if (!fromFile.exists()) { throw new ServiceException(fromPath + \"\u4e0d\u5b58\u5728\uff01\"); } try (FileOutputStream outputStream = new FileOutputStream(toFile); CheckedOutputStream checkedOutputStream = new CheckedOutputStream(outputStream, new CRC32()); ZipOutputStream zipOutputStream = new ZipOutputStream(checkedOutputStream)) { String baseDir = \"\"; compress(fromFile, zipOutputStream, baseDir); } } private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException { if (file.isDirectory()) { compressDirectory(file, zipOut, baseDir); } else { if (baseDir.equals(\"packs\" + File.separator)) { baseDir = File.separator; } else if (baseDir.equals(\"packs\" + File.separator + \"examineeInfo\" + File.separator)) { baseDir = \"examineeInfo\" + File.separator; } compressFile(file, zipOut, baseDir); } } private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException { if (!file.exists()) { return; } try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { ZipEntry entry = new ZipEntry(baseDir + file.getName()); zipOut.putNextEntry(entry); int count; byte[] data = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipOut.write(data, 0, count); } } } private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException { File[] files = dir.listFiles(); if (files != null && ArrayUtils.isNotEmpty(files)) { for (File file : files) { compress(file, zipOut, baseDir + dir.getName() + File.separator); } } }<\/code><\/pre>\n

\u8fd9\u91cc\u4e3b\u8981\u662f\u6709\u7b2c\u4e8c\u4e2acompress\u65b9\u6cd5\u6765\u5904\u7406\uff1a<\/h4>\n

\u7531\u4e8e\u6587\u4ef6\u5939\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n

 \u8fd9\u4e2a\u65b9\u6cd5\u5728\u6253\u5305\u65f6\uff0c\u5c06\u5916\u5c42\u7684\u6587\u4ef6\u5939\u53bb\u6389\uff0c\u53ea\u4fdd\u7559\u5185\u90e8\u6587\u4ef6\u5939<\/p>\n

private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException {         if (file.isDirectory()) {             compressDirectory(file, zipOut, baseDir);         } else { \/\/\u5982\u679c\u662f\u6587\u4ef6\u5939\uff0c\u8fdb\u6765\u5224\u65ad\uff0c\u662f\u6700\u5916\u5c42packs\u6587\u4ef6\u5939\u8fd8\u662f\u5185\u90e8\u7684examineeInfo\u6587\u4ef6\u5939 \/\/\u662f\u6700\u5916\u5c42\u7684\u5219\u5ffd\u7565\u6389\uff0c\u662f\u5185\u90e8\u7684\u5219\u628a\u5916\u5c42\u6587\u4ef6\u5939\u540d\u79f0\u53bb\u6389             if (baseDir.equals(\"packs\" + File.separator)) {                 baseDir = File.separator;             } else if (baseDir.equals(\"packs\" + File.separator + \"examineeInfo\" + File.separator)) {                 baseDir = \"examineeInfo\" + File.separator;             }             compressFile(file, zipOut, baseDir);         }     }<\/code><\/pre>\n

\u8fd9\u6837\u538b\u7f29\u540e\u7684zip\u5305\u89e3\u538b\u540e\uff0c\u76f4\u63a5\u51fa\u73b0\u6587\u4ef6\uff0c\u6ca1\u6709\u6700\u5916\u5c42\u6587\u4ef6\u5939\uff1a<\/p>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n

3.\u538b\u7f29\u65f6\u7ed9zip\u5305\u6dfb\u52a0\u5bc6\u7801\uff1a<\/h3>\n

\u6253\u51fa\u4e00\u4e2a\u5e26\u89e3\u538b\u5bc6\u7801\u7684zip\u5305\uff1a<\/h4>\n

\u7a0b\u5e8f\u8fd0\u884c\u793a\u4f8b\u622a\u56fe\uff1a <\/p>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n

\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n

public static void main(String[] args) { \/\/\u968f\u673a\u5bc6\u7801\u751f\u6210\u5de5\u5177 String ownerPassword = PdfEncryptUtil.getCharAndNum(20); \/\/\u6253\u5305\u76ee\u6807\u6587\u4ef6\u5939 String hallFilePath = \"E:\" + File.separator+ \"packs\"; \/\/\u538b\u7f29\u5305\u8f93\u51fa\u6587\u4ef6\u5939 String outPath = \"E:\" +File.separator+ \"examineeInfo.zip\"; \/\/\u8c03\u7528\u538b\u7f29\u65b9\u6cd5zip\u8fdb\u884c\u538b\u7f29\uff0c\u53c2\u6570\u542b\u4e49\u4e0b\u9762\u4ecb\u7ecd\uff0c\u8fd4\u56de\u7684\u662f\u538b\u7f29\u5305\u7684\u8f93\u51fa\u8def\u5f84 String zipPath = zip(hallFilePath, outPath, false, \"ceshi\"); \/\/\u6253\u5370\u51fa\u6765 System.out.println(zipPath); }<\/code><\/pre>\n

zip\u65b9\u6cd5\u5de5\u5177\u4e2d\u7684\u65b9\u6cd5\uff1a<\/p>\n

\/** * \u4f7f\u7528\u7ed9\u5b9a\u5bc6\u7801\u538b\u7f29\u6307\u5b9a\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u5230\u6307\u5b9a\u4f4d\u7f6e. * <p> * dest\u53ef\u4f20\u6700\u7ec8\u538b\u7f29\u6587\u4ef6\u5b58\u653e\u7684\u7edd\u5bf9\u8def\u5f84,\u4e5f\u53ef\u4ee5\u4f20\u5b58\u653e\u76ee\u5f55,\u4e5f\u53ef\u4ee5\u4f20null\u6216\u8005\"\" * \u5982\u679c\u4f20null\u6216\u8005\"\"\u5219\u5c06\u538b\u7f29\u6587\u4ef6\u5b58\u653e\u5728\u5f53\u524d\u76ee\u5f55,\u5373\u8ddf\u6e90\u6587\u4ef6\u540c\u76ee\u5f55,\u538b\u7f29\u6587\u4ef6\u540d\u53d6\u6e90\u6587\u4ef6\u540d,\u4ee5.zip\u4e3a\u540e\u7f00; * \u5982\u679c\u4ee5\u8def\u5f84\u5206\u9694\u7b26(File.separator)\u7ed3\u5c3e,\u5219\u89c6\u4e3a\u76ee\u5f55,\u538b\u7f29\u6587\u4ef6\u540d\u53d6\u6e90\u6587\u4ef6\u540d,\u4ee5.zip\u4e3a\u540e\u7f00,\u5426\u5219\u89c6\u4e3a\u6587\u4ef6\u540d. * * @param src \u8981\u538b\u7f29\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u8def\u5f84 * @param dest \u538b\u7f29\u6587\u4ef6\u5b58\u653e\u8def\u5f84 * @param isCreateDir \u662f\u5426\u5728\u538b\u7f29\u6587\u4ef6\u91cc\u521b\u5efa\u76ee\u5f55,\u4ec5\u5728\u538b\u7f29\u6587\u4ef6\u4e3a\u76ee\u5f55\u65f6\u6709\u6548. * \u5982\u679c\u4e3afalse,\u5c06\u76f4\u63a5\u538b\u7f29\u76ee\u5f55\u4e0b\u6587\u4ef6\u5230\u538b\u7f29\u6587\u4ef6. * @param passwd \u538b\u7f29\u4f7f\u7528\u7684\u5bc6\u7801 * @return \u6700\u7ec8\u7684\u538b\u7f29\u6587\u4ef6\u5b58\u653e\u7684\u7edd\u5bf9\u8def\u5f84, \u5982\u679c\u4e3anull\u5219\u8bf4\u660e\u538b\u7f29\u5931\u8d25. *\/ public static String zip(String src, String dest, boolean isCreateDir, String passwd) { File srcFile = new File(src); dest = buildDestinationZipFilePath(srcFile, dest); ZipParameters parameters = new ZipParameters(); \/\/ \u538b\u7f29\u65b9\u5f0f parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); \/\/ \u538b\u7f29\u7ea7\u522b parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); if (!StringUtils.isEmpty(passwd)) { parameters.setEncryptFiles(true); \/\/ \u52a0\u5bc6\u65b9\u5f0f parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); parameters.setPassword(passwd.toCharArray()); } try { ZipFile zipFile = new ZipFile(dest); if (srcFile.isDirectory()) { \/\/ \u5982\u679c\u4e0d\u521b\u5efa\u76ee\u5f55\u7684\u8bdd,\u5c06\u76f4\u63a5\u628a\u7ed9\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u538b\u7f29\u5230\u538b\u7f29\u6587\u4ef6,\u5373\u6ca1\u6709\u76ee\u5f55\u7ed3\u6784 if (!isCreateDir) { File[] subFiles = srcFile.listFiles(); for (int i = 0; i < subFiles.length; i++) { if (subFiles[i].isDirectory()) { zipFile.addFolder(subFiles[i], parameters); } else { ArrayList<File> temp = new ArrayList<File>(); Collections.addAll(temp, subFiles[i]); zipFile.addFiles(temp, parameters); } } return dest; } zipFile.addFolder(srcFile, parameters); } else { zipFile.addFile(srcFile, parameters); } return dest; } catch (ZipException e) { e.printStackTrace(); } return null; } private static String buildDestinationZipFilePath(File srcFile, String destParam) { if (StringUtils.isEmpty(destParam)) { if (srcFile.isDirectory()) { destParam = srcFile.getParent() + File.separator + srcFile.getName() + \".zip\"; } else { String fileName = srcFile.getName().substring(0, srcFile.getName().lastIndexOf(\".\")); destParam = srcFile.getParent() + File.separator + fileName + \".zip\"; } } else { \/\/ \u5728\u6307\u5b9a\u8def\u5f84\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u5c06\u5176\u521b\u5efa\u51fa\u6765 createDestDirectoryIfNecessary(destParam); if (destParam.endsWith(File.separator)) { String fileName = \"\"; if (srcFile.isDirectory()) { fileName = srcFile.getName(); } else { fileName = srcFile.getName().substring(0, srcFile.getName().lastIndexOf(\".\")); } destParam += fileName + \".zip\"; } } return destParam; } private static void createDestDirectoryIfNecessary(String destParam) { File destDir = null; if (destParam.endsWith(File.separator)) { destDir = new File(destParam); } else { destDir = new File(destParam.substring(0, destParam.lastIndexOf(File.separator))); } if (!destDir.exists()) { destDir.mkdirs(); } }<\/code><\/pre>\n

\u8fd9\u91cc\u6253\u5305\u51fa\u6765\u7684\u52a0\u5bc6zip\u5305\u4e5f\u662f\u89e3\u538b\u5f00\u5c31\u662f\u6587\u4ef6\u5217\u8868\u7684\u683c\u5f0f\uff0c\u6ca1\u6709\u6700\u5916\u5c42\u6587\u4ef6\u5939\u5305\u88f9\uff0c<\/span><\/strong>\u5982\u679c\u60f3\u8981\u6700\u5916\u5c42\u6587\u4ef6\u5939\u5305\u88f9\uff0c\u53ea\u9700\u8981\u6539\u52a8\u4e0bzip\u65b9\u6cd5\u7684try\u5757\u4e2d\u7684if...else\u8bed\u53e5\u5373\u53ef\uff1a<\/p>\n

try { ZipFile zipFile = new ZipFile(dest); if (srcFile.isDirectory()) { \/\/ \u5982\u679c\u4e0d\u521b\u5efa\u76ee\u5f55\u7684\u8bdd,\u5c06\u76f4\u63a5\u628a\u7ed9\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u538b\u7f29\u5230\u538b\u7f29\u6587\u4ef6,\u5373\u6ca1\u6709\u76ee\u5f55\u7ed3\u6784 if (!isCreateDir) { File[] subFiles = srcFile.listFiles(); ArrayList<File> temp = new ArrayList<File>(); Collections.addAll(temp, subFiles); zipFile.addFiles(temp, parameters); return dest; } zipFile.addFolder(srcFile, parameters); } else { zipFile.addFile(srcFile, parameters); } return dest; } catch (ZipException e) { e.printStackTrace(); }<\/code><\/pre>\n

\u751f\u6210\u6548\u679c\uff1a<\/p>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n

\"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d<\/p>\n<\/p>\n

\u6ce8\u610f\uff1a<\/h3>\n

1.zip\u5305\u8f93\u51fa\u5730\u5740\u5728\u5b9a\u4e49\u65f6\uff0c\u4e00\u5b9a\u8981\u4ee5.zip\u7ed3\u5c3e\uff0c\u5426\u5219\u4f1a\u51fa\u9519\u3002<\/strong><\/p>\n

2.\u4ee3\u8868\u6587\u4ef6\u5939\u5c42\u7ea7\u5173\u7cfb\u7684\/\u5728\u6709\u7684\u65b9\u6cd5\u4e2d\uff0c\u6216\u8005linux\u670d\u52a1\u5668\u4e0a\u4e0d\u8bc6\u522b\uff0c\u6700\u597d\u4f7f\u7528File.separator\u6765\u8868\u793a\u5c42\u7ea7\uff0c\u4e0a\u9762\u6211\u4e5f\u6709\u7528\u5230\u8fc7\u3002<\/strong><\/p>\n

3.\u5bc6\u7801\u6700\u597d\u4f7f\u7528\u52a8\u6001\u751f\u6210\u7684\u5bc6\u7801\uff0c\u6765\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/strong><\/p>\n<\/p>\n

\u538b\u7f29\u7684\u65b9\u6cd5\u5f88\u591a\uff0c\u5de5\u4f5c\u4e4b\u4f59\u5306\u5306\u8bb0\u5f55\uff0c\u80af\u5b9a\u6709\u4e0d\u662f\u5f88\u5408\u7406\u5f88\u5168\u9762\u7684\u5730\u65b9\uff0c\u6709\u5927\u4f6c\u770b\u5230\u6b22\u8fce\u6307\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"java\u5c06\u6587\u4ef6\u538b\u7f29\u6210zip_java\u5e38\u7528jar\u5305\u4e0b\u8f7d\u4f7f\u7528Java\u538b\u7f29\u6587\u4ef6\u751f\u6210zip\u5305\u7684\u603b\u7ed3\uff1a1.\u751f\u6210\u666e\u901azip\u5305\uff0c\u89e3\u538b\u540e\u4e0e\u539f\u6587\u4ef6\u4e00\u81f4\u30022.\u6ce8\u610f\u4f7f\u7528...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5700"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5700"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5700\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}