文件的压缩与解压缩
Java中提供了ZipOutputStream和GZIPOutputStream类供文件压缩使用。
import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public final class ZipUtil {
private ZipUtil() {
}
/**
* 压缩文件成zip
*
* @param srcDir 压缩文件夹路径
* @param targetDir 压缩后文件的路径名称
* @param KeepDirStructure 是否保留原来的目录结构
* @throws RuntimeException
*/
public static void toZip(String srcDir, String targetDir, boolean KeepDirStructure) throws RuntimeException {
File sourceFile = new File(srcDir);
String sourcePath = sourceFile.getParentFile().toString();
String fileName = sourceFile.getName();
ZipOutputStream zos = null;
try {
FileOutputStream out = null;
if (targetDir != null && !"".equals(targetDir)) {
if (sourceFile.isDirectory()) {
out = new FileOutputStream(new File(sourcePath + "/" + fileName + ".zip"));
} else {
out = new FileOutputStream(new File(sourcePath + "/" + fileName.substring(0, fileName.lastIndexOf('.')) + ".zip"));
}
} else {
out = new FileOutputStream(new File(targetDir));
}
zos = new ZipOutputStream(out);
compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
} catch (Exception e) {
throw new RuntimeException("压缩失败!", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 解压zip文件
*
* @param srcFile 待解压的zip文件
* @param descDir 解压后的存放路径
* @throws IOException
**/
public static void unZipFiles(String srcFile, String descDir) throws IOException {
File descDirFile = new File(descDir);
if (!descDirFile.exists()) {
descDirFile.mkdirs();
}
ZipFile zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
for (Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
String outPath = (descDir + "/" + zipEntryName).replaceAll("\\*", "/");
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
if (new File(outPath).isDirectory()) {
continue;
}
try (InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(outPath)) {
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
zipFile.close();
}
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean isDir) throws Exception {
byte[] buf = new byte[2 * 1024];
if (sourceFile.isFile()) {
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
if (isDir) {
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
}
} else {
for (File file : listFiles) {
if (isDir) {
compress(file, zos, name + "/" + file.getName(), isDir);
} else {
compress(file, zos, file.getName(), isDir);
}
}
}
}
}
}
标签:
实用技巧
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2022-11-25 说说Spring如何加载注册BeanDefinition