pom.xml net.lingala.zip4jzip4j1.3.2 java代码: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import net.lingala.zip4j.io.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; /** * 文件压缩 * @author */ public class ZipUtil { public static void main(String[] args) throws Exception { //传递文件 OutputStream out = new FileOutputStream(new File("E:/logs/file"+".zip")); out = zipByFilePath(out, "123456", new String[][]{ {"info/demo1.txt","E:/info/info1.txt"}, {"info2/demo2.txt","E:/info/info2.txt"}}); out.flush(); out.close(); //传递字符流 out = new FileOutputStream(new File("E:/logs/streamt"+".zip")); out = zipByFileInput(out, "123456", new Object[][]{{"info/demo1.txt", new FileInputStream("E:/info/info1.txt")}, {"info2/demo2.txt",new FileInputStream("E:/info/info2.txt")}, {"total.txt",new FileInputStream("E:/info/info2.txt")}}); out.flush(); out.close(); out = new FileOutputStream(new File("E:/logs/fileNoPwd"+".zip")); out = zipByFilePath(out,new String[][]{ {"info/demo1.txt","E:/info/info1.txt"}, {"info2/demo2.txt","E:/info/info2.txt"}}); out.flush(); out.close(); out = new FileOutputStream(new File("E:/logs/streamtNoPwd"+".zip")); out = zipByFileInput(out,new Object[][]{{"info/demo1.txt", new FileInputStream("E:/info/info1.txt")}, {"info2/demo2.txt",new FileInputStream("E:/info/info2.txt")}, {"total.txt",new FileInputStream("E:/info/info2.txt")}}); out.flush(); out.close(); } /** * 压缩文件流-加密 * @param out * @param password * @param fileAry * @return * @throws Exception */ public static OutputStream zipByFilePath(OutputStream out,String password,String[][] fileAry) throws Exception { Object[][] info = new Object[2][fileAry.length]; for(int index=0,size=fileAry.length;index-1) { zipOut.write(btAry,0,byteReadSize); byteReadSize = fileInput.read(btAry); } } return zipOut; } /** * 压缩文件流 * @param out * @param password * @param fileAry {{fileName,InputStream}} * @return * @throws Exception */ public static OutputStream zipByFileInput(OutputStream out,String password,Object[][] fileAry) throws Exception { //压缩的规则设置 ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setSourceExternalStream(true); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(password); ZipOutputStream zipOut = new ZipOutputStream(out); for(int index=0,fileSize=fileAry.length;index-1) { zipOut.write(btAry,0,byteReadSize); byteReadSize = fileInput.read(btAry); } zipOut.closeEntry(); fileInput.close(); } /** * 压缩流中放入文件 * @param zipOut * @param parameters * @param fileInput * @throws Exception */ public static void addToZipOut(ZipOutputStream zipOut,ZipParameters parameters,byte[] btAry) throws Exception { zipOut.write(btAry); zipOut.closeEntry(); } }
转载于:https://www.cnblogs.com/freeLess/p/5458148.html