java 版本压缩、解压缩zip
2024-08-28 18:02:14
import java.io.*;
import java.util.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class FileMgr {
public FileMgr() {
}
/**
* 压缩文件
* @param srcfile File[] 需要压缩的文件列表
* @param zipfile File 压缩后的文件
*/
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Compress the files
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
System.out.println("压缩完成.");
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解压缩
* @param zipfile File 需要解压缩的文件
* @param descDir String 解压后的目标目录
*/
public static void UnZipFiles(java.io.File zipfile, String descDir) {
try {
// Open the ZIP file
ZipFile zf = new ZipFile(zipfile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements(); ) {
// Get the entry name
ZipEntry entry = ( (ZipEntry) entries.nextElement());
String zipEntryName = entry.getName();
InputStream in = zf.getInputStream(entry);
// System.out.println(zipEntryName);
OutputStream out = new FileOutputStream(descDir + zipEntryName);
byte[] buf1 = new byte[1024];
int len;
while ( (len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
// Close the file and stream
in.close();
out.close();
System.out.println("解压缩完成.");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
最新文章
- Android网络操作的几种方法
- Scrum Master 面试题 – 你必须知道的22个Scrum基础知识
- IOS 区分缓存 内存 物理存储 逻辑存储
- 关于PKCS5Padding与PKCS7Padding的区别
- mac brew 安装包下载失败解决
- android设置组件透明度
- 3. JavaScript 数据类型
- 谈JavaScript的继承
- Linux嵌入式开发中常用的两个工具
- Azure 认知服务 (2) 计算机视觉API - 分析图像
- Java数据结构和算法 - 高级排序
- ubuntu下adb的使用以及开启黑域
- [一]FileDescriptor文件描述符 标准输入输出错误 文件描述符
- python 配置文件返回的两种方式,写法不一样而已
- 导入dmp文件时,需要删除原有ORACLE数据库实例
- 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型
- HDU 6092 Rikka with Subset(dp)
- CSS样式属性——字体+文本
- (转载)Hibernate的事务管理
- [JS]1.Hello Word