使⽤commons-compress操作zip⽂件(压缩和解压缩)
Apache Commons Compress是⼀个压缩、解压缩⽂件的类库。
可以操作ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2格式的⽂件,功能⽐较强⼤。
在这⾥写两个⽤Commons Compress把⽂件压缩成zip和从zip解压缩的⽅法。
直接贴上⼯具类代码:
/**
* Zip⽂件⼯具类
* @author Luxh
苏菲的杰作
*/
public class ZipFileUtil {
/**
* 把⽂件压缩成zip格式
* @param files 需要压缩的⽂件
* @param zipFilePath 压缩后的zip⽂件路径 ,如"D:/test/aa.zip";
*/
public static void compressFiles2Zip(File[] files,String zipFilePath) {
if(files != null && files.length >0) {
if(isEndsWithZip(zipFilePath)) {
ZipArchiveOutputStream zaos = null;
try {
File zipFile = new File(zipFilePath);
zaos = new ZipArchiveOutputStream(zipFile);
创新名言
/
/U Zip64 extensions for all entries where they are required
zaos.tUZip64(Zip64Mode.AsNeeded);
//将每个⽂件⽤ZipArchiveEntry封装
//再⽤ZipArchiveOutputStream写到压缩⽂件中
for(File file : files) {
if(file != null) {
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(Name());
zaos.putArchiveEntry(zipArchiveEntry);
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024 * 5];
int len = -1;
while((len = is.read(buffer)) != -1) {家庭必备生活用品
//把缓冲区的字节写⼊到ZipArchiveEntry
zaos.write(buffer, 0, len);
}
//Writes all necessary data for this entry.
zaos.cloArchiveEntry();
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
if(is != null)
is.clo();
}
}
}
zaos.finish();
}catch(Exception e){
throw new RuntimeException(e);
}finally {
try {
if(zaos != null) {
if(zaos != null) {
zaos.clo();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
普通话培训内容}
}
}
}
/**
* 把zip⽂件解压到指定的⽂件夹
* @param zipFilePath zip⽂件路径, 如 "D:/test/aa.zip"
* @param saveFileDir 解压后的⽂件存放路径, 如"D:/test/"
*/
public static void decompressZip(String zipFilePath,String saveFileDir) {
if(isEndsWithZip(zipFilePath)) {
File file = new File(zipFilePath);
ists()) {
InputStream is = null;
//can read Zip archives
ZipArchiveInputStream zais = null;
try {
is = new FileInputStream(file);
zais = new ZipArchiveInputStream(is);
ArchiveEntry archiveEntry = null;
加油怎么画//把zip包中的每个⽂件读取出来
//然后把⽂件写到指定的⽂件夹
while((archiveEntry = NextEntry()) != null) {
//获取⽂件名
保生String entryFileName = Name();
//构造解压出来的⽂件存放路径
String entryFilePath = saveFileDir + entryFileName;
byte[] content = new byte[(int) Size()];
OutputStream os = null;
try {
//把解压出来的⽂件写到指定路径
File entryFile = new File(entryFilePath);
os = new BufferedOutputStream(new FileOutputStream(entryFile)); os.write(content);
}catch(IOException e) {
throw new IOException(e);《水浒传》读后感
}finally {
if(os != null) {
os.flush();
os.clo();
}
}
}
}catch(Exception e) {
throw new RuntimeException(e);
}finally {
try {
if(zais != null) {
zais.clo();
}
if(is != null) {
is.clo();
}
} catch (IOException e) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
/**
* 判断⽂件名是否以.zip为后缀
* @param fileName 需要判断的⽂件名
* @return是zip⽂件返回true,否则返回fal
*/
public static boolean isEndsWithZip(String fileName) {
boolean flag = fal;
if(fileName != null && !"".im())) {
dsWith(".ZIP")||dsWith(".zip")){ flag = true;
}
}
return flag;
}
}
测试代码:
[java] view plain copy print?
package st;
import java.io.File;
import org.junit.Test;
import cn.luxh.utils.ZipFileUtil;
/**
* 测试ZipFileUtil的压缩和解压缩⽅法
* @author Luxh
*/
public class ZipFileUtilTest {
@Test
public void testCompressFiles2Zip() {
//存放待压缩⽂件的⽬录
File srcFile = new File("D:/test");
//压缩后的zip⽂件路径
String zipFilePath = "d:/test2/test.zip";
ists()) {
File[] files = srcFile.listFiles();
}
}
@Test
public void testDecompressZip() {
//压缩包所在路径
String zipFilePath = "d:/test2/test.zip";
//解压后的⽂件存放⽬录
String saveFileDir = "d:/test2/";
//调⽤解压⽅法
党风廉洁建设内容ZipFileUtil.decompressZip(zipFilePath, saveFileDir);
}
}