官方地址:(需要FQ)
jar包:
演示包:
一、压缩
1、标准压缩
ZipFile zipFile = new ZipFile("c:\\date.zip");ArrayList filesToAdd = new ArrayList();filesToAdd.add(new File(csvFilePath));ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); //设置密码 parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); parameters.setPassword("123456"); zipFile.addFiles(filesToAdd, parameters);
2、AES加密
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test3.zip");ArrayList filesToAdd = new ArrayList();filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); //设置密码parameters.setEncryptFiles(true);parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);parameters.setPassword("123456!");zipFile.addFiles(filesToAdd, parameters);
3、文件夹压缩
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test4.zip");String folderToAdd = "c:\\ZipTest";ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);zipFile.addFolder(folderToAdd, parameters);
4、将文件压缩到指定文件夹中
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test5.zip");ArrayList filesToAdd = new ArrayList();filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);parameters.setRootFolderInZip("test2/");zipFile.addFiles(filesToAdd, parameters);
5、向ZIP中添加文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip");ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);parameters.setFileNameInZip("文件2.doc");parameters.setSourceExternalStream(true);InputStream is = new FileInputStream("c:\\ZipTest\\文件2.doc");zipFile.addStream(is, parameters);is.close();
6、分隔压缩文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test7.zip");ArrayList filesToAdd = new ArrayList();filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipFile.createZipFile(filesToAdd, parameters, true, 65536);
7、创建ZIP流
ArrayList filesToAdd = new ArrayList();filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(new File("c:\\ZipTest\\test8.zip")));ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);// Set passwordparameters.setEncryptFiles(true);parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);parameters.setPassword("test123!");for (int i = 0; i < filesToAdd.size(); i++) { File file = (File)filesToAdd.get(i); outputStream.putNextEntry(file,parameters); if (file.isDirectory()) { outputStream.closeEntry(); continue; } InputStream inputStream = new FileInputStream(file); byte[] readBuff = new byte[4096]; int readLen = -1; while ((readLen = inputStream.read(readBuff)) != -1) { outputStream.write(readBuff, 0, readLen); } outputStream.closeEntry(); inputStream.close();}outputStream.finish();outputStream.close();
8、从ZIP中删除文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip");//删除指定文件zipFile.removeFile("sample.txt");//删除第一个文件if (zipFile.getFileHeaders() != null && zipFile.getFileHeaders().size() > 0) { zipFile.removeFile((FileHeader)zipFile.getFileHeaders().get(0));} else { System.out.println("This cannot be demonstrated as zip file does not have any files left");}
9、获取ZIP中文件一览
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test5.zip");List fileHeaderList = zipFile.getFileHeaders();for (int i = 0; i < fileHeaderList.size(); i++) { FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); System.out.println("****File Details for: " + fileHeader.getFileName() + "*****"); System.out.println("Name: " + fileHeader.getFileName()); System.out.println("Compressed Size: " + fileHeader.getCompressedSize()); System.out.println("Uncompressed Size: " + fileHeader.getUncompressedSize()); System.out.println("CRC: " + fileHeader.getCrc32()); System.out.println("************************************************************");}
二、解压
1、解压所有文件
//方法一ZipFile zipFile = new ZipFile("c:\\ZipTest\\test1.zip"); zipFile.extractAll("c:\\ZipTest1");//方法二ZipFile zipFile2 = new ZipFile("c:\\ZipTest\\test2.zip"); if (zipFile2.isEncrypted()) { zipFile2.setPassword("test123!");}List fileHeaderList = zipFile2.getFileHeaders();for (int i = 0; i < fileHeaderList.size(); i++) { FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); zipFile2.extractFile(fileHeader, "c:\\ZipTest2\\");}
2、解压所有文件到流
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip"); if (zipFile.isEncrypted()) { zipFile.setPassword("test123!");}List fileHeaderList = zipFile.getFileHeaders();for (int i = 0; i < fileHeaderList.size(); i++) { FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); if (fileHeader != null) { String outFilePath = "c:\\ZipTest3\\" + System.getProperty("file.separator") + fileHeader.getFileName(); File outFile = new File(outFilePath); if (fileHeader.isDirectory()) { outFile.mkdirs(); continue; } File parentDir = outFile.getParentFile(); if (!parentDir.exists()) { parentDir.mkdirs(); } ZipInputStream is = zipFile.getInputStream(fileHeader); OutputStream os = new FileOutputStream(outFile); int readLen = -1; byte[] buff = new byte[4096]; while ((readLen = is.read(buff)) != -1) { os.write(buff, 0, readLen); } os.close(); os = null; is.close(); is = null; UnzipUtil.applyFileAttributes(fileHeader, outFile); System.out.println("Done extracting: " + fileHeader.getFileName()); } else { System.err.println("fileheader is null. Shouldn't be here"); }}
3、解压单个文件
ZipFile zipFile = new ZipFile("c:\\ZipTest\\test2.zip");if (zipFile.isEncrypted()) { zipFile.setPassword("test123!");}zipFile.extractFile("文件.doc", "c:\\ZipTest4\\");