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();
}
}
}

最新文章

  1. Android网络操作的几种方法
  2. Scrum Master 面试题 – 你必须知道的22个Scrum基础知识
  3. IOS 区分缓存 内存 物理存储 逻辑存储
  4. 关于PKCS5Padding与PKCS7Padding的区别
  5. mac brew 安装包下载失败解决
  6. android设置组件透明度
  7. 3. JavaScript 数据类型
  8. 谈JavaScript的继承
  9. Linux嵌入式开发中常用的两个工具
  10. Azure 认知服务 (2) 计算机视觉API - 分析图像
  11. Java数据结构和算法 - 高级排序
  12. ubuntu下adb的使用以及开启黑域
  13. [一]FileDescriptor文件描述符 标准输入输出错误 文件描述符
  14. python 配置文件返回的两种方式,写法不一样而已
  15. 导入dmp文件时,需要删除原有ORACLE数据库实例
  16. 无法将 lambda 表达式 转换为类型“System.Delegate”,因为它不是委托类型
  17. HDU 6092 Rikka with Subset(dp)
  18. CSS样式属性——字体+文本
  19. (转载)Hibernate的事务管理
  20. [JS]1.Hello Word

热门文章

  1. 网络之XML解析-GData
  2. CentOS 7.2安装Docker-ce
  3. jQuery在线引用地址(全)
  4. Jquey节点操作
  5. .NET 简单导出CSV文件
  6. ubuntu16.4+nginx+uwsgi+Django 部署上线
  7. 安装nvm管理不同的node版本
  8. 关于animation的一些简单基础和使用方法记载
  9. vue中监听window.resize的变化
  10. FI配置步骤清单.枫