博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA利用Zip4j解压缩【转】
阅读量:6082 次
发布时间:2019-06-20

本文共 6811 字,大约阅读时间需要 22 分钟。

官方地址:(需要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\\");

源文地址:

 

 

 

 

 

转载地址:http://hazwa.baihongyu.com/

你可能感兴趣的文章
hdu 2191 (多重背包+二进制优化)
查看>>
home.php
查看>>
neo4j---删除关系和节点
查看>>
redis分布式锁redisson
查看>>
什么样的企业可以称之为初创企业?
查看>>
Python爬虫之BeautifulSoup
查看>>
《HTML 5与CSS 3权威指南(第3版·下册)》——第20章 使用选择器在页面中插入内容...
查看>>
如何判断自己适不适合做程序员?这几个特点了解一下
查看>>
newinstance()和new有什么区别
查看>>
android下载封装类
查看>>
[node] 用 node-webkit 开发桌面应用
查看>>
Nginx访问控制和虚拟主机
查看>>
report widget not working for external users
查看>>
windows phone 摄像头得到图片是旋转90°
查看>>
Linux--sed使用
查看>>
没有显示器的情况下安装和使用树莓派
查看>>
Q85 最大矩形
查看>>
【android】使用handler更新UI
查看>>
mochiweb 源码阅读(十五)
查看>>
前端面试中的常见的算法问题
查看>>