//获取文件路径:sourceFilePath -> /Users/vincent/IDEA_Project/my_project/zip/dirtest/jdkziptest/srcfile.txt Path sourceFilePath = Paths.get(filePath); if (Files.notExists(sourceFilePath)) { thrownew NoSuchFileException(filePath); } if (!sourceFilePath.toFile().isFile()) { thrownew NoSuchFileException(filePath + " is not a file..."); }
//获取文件目录路径:dirPath -> /Users/vincent/IDEA_Project/my_project/zip/dirtest/jdkziptest/out Path sourceDirPath = Paths.get(desDirPath); if (Files.notExists(sourceDirPath)) { thrownew NotDirectoryException(desDirPath); } if (!Files.isDirectory(sourceDirPath)) { thrownew NotDirectoryException(desDirPath + " is not a directory..."); }
/** * @author vincent */ publicclassZip4jTest{ @Test publicvoidpackAddFile()throws ZipException { /* * 添加具体文件(该文件必须存在,否则会抛异常)到压缩文件中 */ //官网案例:Creating a zip file with single file in it / Adding single file to an existing zip new ZipFile("dirtest/zip4jtest/filename.zip").addFile("dirtest/zip4jtest/src/a.txt"); new ZipFile("dirtest/zip4jtest/filename.zip").addFile(new File("dirtest/zip4jtest/src/b.txt"));
//官网案例:Creating a zip file with multiple files / Adding multiple files to an existing zip new ZipFile("dirtest/zip4jtest/filename.zip").addFiles(Arrays.asList(new File("dirtest/zip4jtest/src/first_file"), new File("dirtest/zip4jtest/src/second_file"))); }
@Test publicvoidpackAddFolder()throws ZipException { /* * 添加具体文件夹(该文件夹必须存在,否则会抛异常)到压缩文件中(被添加的文件夹,可以设置过滤条件) */ //官网案例:Creating a zip file by adding a folder to it / Adding a folder to an existing zip new ZipFile("dirtest/zip4jtest/filenamefolder.zip").addFolder(new File("dirtest/zip4jtest/srcdir2"));
//官网案例:Since v2.6, it is possible to exclude certain files when adding a folder to zip by using an ExcludeFileFilter //ExcludeFileFilter:过滤掉文件夹内不需要的文件(该案例是过滤文件名结尾是 ext 格式的文件) ExcludeFileFilter excludeFileFilter = file -> FilenameUtils.isExtension(file.getName(), "ext"); ZipParameters zipParameters = new ZipParameters(); zipParameters.setExcludeFileFilter(excludeFileFilter);
//CompressionMethod:压缩方式 // zipParameters.setCompressionMethod(CompressionMethod.STORE); new ZipFile("dirtest/zip4jtest/excludefilefilter.zip").addFolder(new File("dirtest/zip4jtest/srcdir3"), zipParameters); }
@Test publicvoidpackWithPwd()throws ZipException { /* * 压缩文件并设置保护密码,或者添加文件到要以存在的压缩文件中 */ //官网案例:Creating a password protected zip file / Adding files to an existing zip with password protection ZipParameters zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); // Below line is optional. AES 256 is used by default. You can override it to use AES 128. AES 192 is supported only for extracting. zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
List<File> filesToAdd = Arrays.asList( new File("dirtest/zip4jtest/src/somefile.txt"), new File("dirtest/zip4jtest/src/someotherfile.txt") );
ZipFile zipFile = new ZipFile("dirtest/zip4jtest/filenamepwd.zip", "password".toCharArray()); zipFile.addFiles(filesToAdd, zipParameters); }
@Test publicvoidunpackAllFile()throws ZipException { /* * 从 zip 提取所有文件 */ //官网案例:Extracting all files from a zip new ZipFile("dirtest/zip4jtest/filename.zip").extractAll("dirtest/zip4jtest/out"); }
@Test publicvoidunpackAllFileWithPwd()throws ZipException { /* * 从受密码保护的 zip 提取所有文件 */ //官网案例:Extracting all files from a password protected zip new ZipFile("dirtest/zip4jtest/filenamepwd.zip", "password".toCharArray()).extractAll("dirtest/zip4jtest/out"); }
@Test publicvoidunpackSingleFile()throws ZipException { /* * 从 zip 提取单个文件 */ //官网案例:Extracting a single file from zip new ZipFile("dirtest/zip4jtest/filename.zip").extractFile("a.txt", "dirtest/zip4jtest/out"); }
@Test publicvoidunpackSingleFileWithPwd()throws ZipException { /* * 从受密码保护的 zip 提取单个文件 */ //官网案例:Extracting a single file from zip which is password protected new ZipFile("dirtest/zip4jtest/filenamepwd.zip", "password".toCharArray()).extractFile("somefile.txt", "dirtest/zip4jtest/out"); }
if (Files.notExists(Paths.get(sourceZipFilePath))) { thrownew NoSuchFileException(sourceZipFilePath); } if (!Paths.get(sourceZipFilePath).toFile().isFile()) { thrownew NoSuchFileException(sourceZipFilePath + " is not a file..."); }
if (Files.notExists(Paths.get(extractedZipDirPath))) { thrownew NotDirectoryException(extractedZipDirPath); } if (!Files.isDirectory(Paths.get(extractedZipDirPath))) { thrownew NotDirectoryException(extractedZipDirPath + " is not a directory..."); }
ZipFile zipFile = new ZipFile(sourceZipFilePath); if (zipFile.isEncrypted()) { zipFile.setPassword(password.toCharArray()); } zipFile.extractAll(extractedZipDirPath); } }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !