java自行车火多重,Java解压多重zip压缩包

转自https://blog.csdn.net/mahoking/article/details/54882783

Apache Commons是Apache软件基金会的项目,曾隶属于Jakarta项目。Commons的目的是提供可重用的、开源的Java代码。Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。

Compress

本例主要介绍组件Compress,Compress是ApacheCommons提供压缩、解压缩文件的类库,可以操作ar, cpio, Unix dump, tar, zip,gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大。本例主要演示Compress操作对zip文件压缩与解压缩的操作。

/**

* 将文件打包成zip压缩包文件

*/

private static void compressFiles2Zip(){

long startInt = System.currentTimeMillis();

File file1 = new File("D:\\logs\\log4j.log");

File file2 = new File("D:\\logs\\log4j.log.2016-07-31-19");

List files = new ArrayList();

files.add(file1);

files.add(file2);

File zipFile = new File("D:\\logs\\log4j.log.zip");

InputStream inputStream = null;

ZipArchiveOutputStream zipArchiveOutputStream = null;

try {

zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);

zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);

for(File file : files){

//将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件

ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, "test"+File.separator+file.getName());

zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);

inputStream = new FileInputStream(file);

byte[] buffer = new byte[1024 * 5];

int len = -1;

while((len = inputStream.read(buffer)) != -1) {

//把缓冲区的字节写入到ZipArchiveEntry

zipArchiveOutputStream.write(buffer, 0, len);

}

}

zipArchiveOutputStream.closeArchiveEntry();

zipArchiveOutputStream.finish();

} catch (IOException e) {

//            e.printStackTrace();

}finally{

//关闭输入流

if(null!=inputStream){

try {

inputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

//关闭输出流

if(null!=zipArchiveOutputStream){

try {

zipArchiveOutputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

}

System.out.println("耗时:"+(System.currentTimeMillis()-startInt)+"毫秒");

}

----------

/**

* 将zip压缩包解压成文件到指定文件夹

*/

private static void decompressZip2Files(){

long startInt = System.currentTimeMillis();

String zipFilePath = "D:\\logs\\log4j.log.zip";

File zipFile = new File(zipFilePath);

InputStream inputStream = null;

OutputStream outputStream = null;

//zip文件输入流

ZipArchiveInputStream zipArchiveInputStream = null;

ArchiveEntry archiveEntry = null;

try {

inputStream = new FileInputStream(new File(zipFilePath));

zipArchiveInputStream = new ZipArchiveInputStream(inputStream,"UTF-8");

while(null!=(archiveEntry = zipArchiveInputStream.getNextEntry())){

//获取文件名

String archiveEntryFileName = archiveEntry.getName();

//构造解压后文件的存放路径

String archiveEntryPath = "D:\\logs\\test\\" + archiveEntryFileName;

byte[] content = new byte[(int) archiveEntry.getSize()];

zipArchiveInputStream.read(content);

//把解压出来的文件写到指定路径

File entryFile  = new File(archiveEntryPath);

if(!entryFile.exists()){

entryFile.getParentFile().mkdirs();

}

outputStream = new FileOutputStream(entryFile);

outputStream.write(content);

outputStream.flush();

}

} catch (FileNotFoundException e) {

//            e.printStackTrace();

} catch (IOException e) {

//            e.printStackTrace();

}finally{

if(null!=outputStream){

try {

outputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

if(null!=zipArchiveInputStream){

try {

zipArchiveInputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

if(null!=inputStream){

try {

inputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

}

System.out.println("耗时:"+(System.currentTimeMillis()-startInt)+"毫秒");

}

--------

package com.mahaochen.apache.commons.Compress;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.commons.compress.archivers.ArchiveEntry;

import org.apache.commons.compress.archivers.zip.Zip64Mode;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

public class CommCompressZipFileUtil {

/**

* 将文件打包成zip压缩包文件

* @param files

* @param targetFilePath

* @return

*/

public boolean compressFiles2Zip(File[] files,String targetFilePath){

InputStream inputStream = null;

ZipArchiveOutputStream zipArchiveOutputStream = null;

try {

File zipFile = new File(targetFilePath);

zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);

//Use Zip64 extensions for all entries where they are required

zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);

for(File file : files){

//将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件

ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());

zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);

inputStream = new FileInputStream(file);

byte[] buffer = new byte[1024 * 5];

int len = -1;

while((len = inputStream.read(buffer)) != -1) {

//把缓冲区的字节写入到ZipArchiveEntry

zipArchiveOutputStream.write(buffer, 0, len);

}

}

zipArchiveOutputStream.closeArchiveEntry();

zipArchiveOutputStream.finish();

} catch (IOException e) {

//            e.printStackTrace();

return false;

}finally{

//关闭输入流

if(null!=inputStream){

try {

inputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

//关闭输出流

if(null!=zipArchiveOutputStream){

try {

zipArchiveOutputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

}

return true;

}

/**

* 将zip压缩包解压成文件到指定文件夹

* @param zipFilePath

* @param targetDirPath

* @return

*/

public boolean decompressZip2Files(String zipFilePath,String targetDirPath){

InputStream inputStream = null;

OutputStream outputStream = null;

//zip文件输入流

ZipArchiveInputStream zipArchiveInputStream = null;

ArchiveEntry archiveEntry = null;

try {

File zipFile = new File(zipFilePath);

inputStream = new FileInputStream(zipFile);

zipArchiveInputStream = new ZipArchiveInputStream(inputStream,"UTF-8");

while(null!=(archiveEntry = zipArchiveInputStream.getNextEntry())){

//获取文件名

String archiveEntryFileName = archiveEntry.getName();

//构造解压后文件的存放路径

String archiveEntryPath = targetDirPath + archiveEntryFileName;

//把解压出来的文件写到指定路径

File entryFile  = new File(archiveEntryPath);

if(!entryFile.exists()){

entryFile.getParentFile().mkdirs();

}

byte[] buffer = new byte[1024 * 5];

outputStream = new FileOutputStream(entryFile);

int len = -1;

while((len = zipArchiveInputStream.read(buffer)) != -1){

outputStream.write(buffer,0,len);

}

outputStream.flush();

}

} catch (FileNotFoundException e) {

//            e.printStackTrace();

return false;

} catch (IOException e) {

//            e.printStackTrace();

return false;

}finally{

if(null!=outputStream){

try {

outputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

if(null!=zipArchiveInputStream){

try {

zipArchiveInputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

if(null!=inputStream){

try {

inputStream.close();

} catch (IOException e) {

//                    e.printStackTrace();

}

}

}

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值