首页 > 编程语言 > 详细

JAVA利用Zip4j解压缩【转】

时间:2016-01-15 16:21:05      阅读:214      评论:0      收藏:0      [点我收藏+]

官方地址:http://www.lingala.net/zip4j/(需要FQ)

jar包:http://pan.baidu.com/s/145hwI

演示包:http://pan.baidu.com/s/1vbFIB

一、压缩

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 password
parameters.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://rensanning.iteye.com/blog/1836727

 

JAVA利用Zip4j解压缩【转】

原文:http://www.cnblogs.com/langtianya/p/5133479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!