📄 ziptool.java
字号:
/**
*
*/
package com.sincere.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
* @author Administrator
*
*/
public class ZipTool {
/**
*
*/
public ZipTool() {
// TODO Auto-generated constructor stub
}
/**
* 解压缩
* @param zipFilePath zip文件路径
* @param releasePath 解压后保存路径
* @throws IOException
*/
public static void decompressFile(String zipFilePath, String releasePath)
throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<ZipEntry> enumeration = zipFile.getEntries();
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
ZipEntry zipEntry = null;
String zipEntryNameStr = "";
String[] zipEntryNameArray = null;
while (enumeration.hasMoreElements()) {
zipEntry = enumeration.nextElement();
zipEntryNameStr = zipEntry.getName();
zipEntryNameArray = zipEntryNameStr.split("/");
String path = releasePath;
File root = new File(releasePath);
if (!root.exists()) {
root.mkdir();
}
for (int i = 0; i < zipEntryNameArray.length; i++) {
if (i < zipEntryNameArray.length - 1) {
//创建目录
path = path + File.separator + zipEntryNameArray[i];
new File(path).mkdir();
} else {
if (zipEntryNameStr.endsWith(File.separator)) {
//创建目录
new File(releasePath + zipEntryNameStr).mkdir();
} else {
inputStream = zipFile.getInputStream(zipEntry);
fileOutputStream = new FileOutputStream(new File(releasePath+ zipEntryNameStr));
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
inputStream.close();
fileOutputStream.close();
}
}
}
}
zipFile.close();
}
/**
* 压缩文件
* @param zipFilePath zip压缩包路径及文件名
* @param releasePath 要压缩的目录(仅支持当前目录下文件)
* @param strComment 压缩文件注释
*/
public void compressionFile(String zipFilePath, String releasePath, String strComment) {
try {
File rFile = new File(releasePath);
if (!rFile.exists()) {
return;
}
//判断是否是一个目录
if (!rFile.isDirectory()) {
return;
}
//打开zip文件输出流
org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(zipFilePath));
out.setComment(strComment);
File[] files = rFile.listFiles();
if (files == null) {
return;
}
byte[] buf = new byte[1024];
for (int i = 0; i < files.length; i++) {
if (files[i] == null)
continue;
//定义输出流
FileInputStream in = new FileInputStream(files[i]);
//添加文件到压缩包
out.putNextEntry(new org.apache.tools.zip.ZipEntry(files[i].getName()));
//写文件
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
//关闭文件流
out.closeEntry();
in.close();
}
out.close();
} catch (Exception e) {
System.out.println("压缩文件发生异常:");
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
long bTime = System.currentTimeMillis();
ZipTool zipTool = new ZipTool();
try {
//zipTool.compressionFile("c:\\1217916021656.zip", "c:\\root\\2008\\党群办\\党群办", "注释");
zipTool.decompressFile("c:\\1217916021656.zip", "c:\\");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("测试用时:" + (System.currentTimeMillis() - bTime));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -