//获取文件路径:sourceFilePath -> /Users/vincent/IDEA_Project/my_project/zip/dirtest/jdkziptest/srcfile.txt PathsourceFilePath= Paths.get(filePath); if (Files.notExists(sourceFilePath)) { thrownewNoSuchFileException(filePath); } if (!sourceFilePath.toFile().isFile()) { thrownewNoSuchFileException(filePath + " is not a file..."); }
//获取文件目录路径:dirPath -> /Users/vincent/IDEA_Project/my_project/zip/dirtest/jdkziptest/out PathsourceDirPath= Paths.get(desDirPath); if (Files.notExists(sourceDirPath)) { thrownewNotDirectoryException(desDirPath); } if (!Files.isDirectory(sourceDirPath)) { thrownewNotDirectoryException(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 newZipFile("dirtest/zip4jtest/filename.zip").addFile("dirtest/zip4jtest/src/a.txt"); newZipFile("dirtest/zip4jtest/filename.zip").addFile(newFile("dirtest/zip4jtest/src/b.txt"));
//官网案例:Creating a zip file with multiple files / Adding multiple files to an existing zip newZipFile("dirtest/zip4jtest/filename.zip").addFiles(Arrays.asList(newFile("dirtest/zip4jtest/src/first_file"), newFile("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 newZipFile("dirtest/zip4jtest/filenamefolder.zip").addFolder(newFile("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 格式的文件) ExcludeFileFilterexcludeFileFilter= file -> FilenameUtils.isExtension(file.getName(), "ext"); ZipParameterszipParameters=newZipParameters(); zipParameters.setExcludeFileFilter(excludeFileFilter);
@Test publicvoidpackWithPwd()throws ZipException { /* * 压缩文件并设置保护密码,或者添加文件到要以存在的压缩文件中 */ //官网案例:Creating a password protected zip file / Adding files to an existing zip with password protection ZipParameterszipParameters=newZipParameters(); 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);
@Test publicvoidunpackAllFile()throws ZipException { /* * 从 zip 提取所有文件 */ //官网案例:Extracting all files from a zip newZipFile("dirtest/zip4jtest/filename.zip").extractAll("dirtest/zip4jtest/out"); }
@Test publicvoidunpackAllFileWithPwd()throws ZipException { /* * 从受密码保护的 zip 提取所有文件 */ //官网案例:Extracting all files from a password protected zip newZipFile("dirtest/zip4jtest/filenamepwd.zip", "password".toCharArray()).extractAll("dirtest/zip4jtest/out"); }
@Test publicvoidunpackSingleFile()throws ZipException { /* * 从 zip 提取单个文件 */ //官网案例:Extracting a single file from zip newZipFile("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 newZipFile("dirtest/zip4jtest/filenamepwd.zip", "password".toCharArray()).extractFile("somefile.txt", "dirtest/zip4jtest/out"); }
publicvoidpack(String filePath)throws ZipException, NoSuchFileException { Objects.requireNonNull(filePath); Pathpath= Paths.get(filePath); if (Files.notExists(path)) { thrownewNoSuchFileException(filePath); } if (!path.toFile().isFile()) { thrownewNoSuchFileException(filePath + " is not a file..."); }
if (Files.notExists(Paths.get(sourceZipFilePath))) { thrownewNoSuchFileException(sourceZipFilePath); } if (!Paths.get(sourceZipFilePath).toFile().isFile()) { thrownewNoSuchFileException(sourceZipFilePath + " is not a file..."); }
if (Files.notExists(Paths.get(extractedZipDirPath))) { thrownewNotDirectoryException(extractedZipDirPath); } if (!Files.isDirectory(Paths.get(extractedZipDirPath))) { thrownewNotDirectoryException(extractedZipDirPath + " is not a directory..."); }
ZipFilezipFile=newZipFile(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 !