首页 > 编程语言 >Java压缩、解压zip文件

Java压缩、解压zip文件

时间:2022-10-02 10:02:16浏览次数:42  
标签:解压 src Java String zip baseDir zos File new


代码:

public class ZipUtil {

/**
* 压缩文件/文件夹
*
*/
public static void compress(String srcFilePath, String destFilePath) {
File src = new File(srcFilePath);
if (!src.exists()) {
throw new RuntimeException(srcFilePath + "不存在");
}
File zipFile = new File(destFilePath);
try {
FileOutputStream fos = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
String baseDir = "";
compressbyType(src, zos, baseDir);
zos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static void compressbyType(File src, ZipOutputStream zos, String baseDir) {
if (!src.exists())
return;
System.out.println("压缩" + baseDir + src.getName());
if (src.isFile()) {
compressFile(src, zos, baseDir);
} else if (src.isDirectory()) {
compressDir(src, zos, baseDir);
}
}


/**
* 压缩文件
*/
private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
if (!file.exists())
return;
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zos.putNextEntry(entry);
int count;
byte[] buf = new byte[8019];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
// TODO: handle exception
}
}


/**
* 压缩文件夹
*
*/
private static void compressDir(File dir, ZipOutputStream zos, String baseDir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
if(files.length == 0){
try {
zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator));
} catch (IOException e) {
e.printStackTrace();
}
}
for (File file : files) {
compressbyType(file, zos, baseDir + dir.getName() + File.separator);
}
}


/**
* 解压文件/文件夹
*/
public static void decompress(String srcPath, String dest) throws Exception {
File file = new File(srcPath);
if (!file.exists()) {
throw new RuntimeException(srcPath + "所指文件不存在");
}
ZipFile zf = new ZipFile(file);
Enumeration entries = zf.entries();
ZipEntry entry = null;
while (entries.hasMoreElements()) {
entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
if (entry.isDirectory()) {
String dirPath = dest + File.separator + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 表示文件
File f = new File(dest + File.separator + entry.getName());
if (!f.exists()) {
//String dirs = FileUtils.getParentPath(f);
String dirs = f.getParent();
File parentDir = new File(dirs);
parentDir.mkdirs();
}
f.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zf.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(f);
int count;
byte[] buf = new byte[8192];
while ((count = is.read(buf)) != -1) {
fos.write(buf, 0, count);
}
is.close();
fos.close();
}
}
}

public static void main(String[] args) throws Exception {
// compress("/Users/liyinlong/lyl/test","/Users/liyinlong/lyl/test.zip");

decompress("/Users/liyinlong/lyl/test.zip", "/Users/liyinlong/lyl/test");
}
}

标签:解压,src,Java,String,zip,baseDir,zos,File,new
From: https://blog.51cto.com/u_12482901/5729214

相关文章

  • java 类与对象
    1.在创造一个类的时候,如果自定义一个构造函数,那么计算机将不再提供默认构造函数。2.在一个类中,如果该类有无参构造函数和有参构造函数,一个成员变量value且带初值,一个valu......
  • Java lamda表达式 Predicate<T>、BiFunction<T,T,R>、FunctionalInterface 应用实例说
    使用相对应的函数式接口,可使编写程序在某些时候变得更高雅和灵活,下面对各种情况进行说明ps:核心原理就是将方法作为一个参数传到另一个方法,使该方法处理内容更丰富和灵......
  • Demo11_12 java流程控制01小总结
    packagecom.HuanXin.scanner;importjava.util.Scanner;publicclassDemo01_02{publicstaticvoidmain(String[]args){//hasNext()与next()Sca......
  • Java中的运算符
    短路1.true||2.true||3.false||4.true其中走完1表达式就不会再走了节省了不必要的计算1.true&&2.true&&3.false&&4.true其中走完3表达式就不会再走了......
  • JAVA包装类
    *包装类与基本数据类型*包装类是将基本数据类型封装成一个类,包含属性和方法*使用:*在使用过程中,会涉及到自动装箱和自动拆箱*装箱:将基本数据类型转换......
  • Java学生管理系统(未使用文件和清屏)
    Java的学生管理系统希望通过前面所学的类的封装知识和String类与ArrayList集合来实现一个简单的学生管理系统功能要求:添加、删除、修改、查看所有、退出注意:为了保证学......
  • Java SE 宋红康 days04-高级篇-枚举类
    1.自定义枚举类a.声明类A对象的属性:private final修饰b.私有化类的构造器,并给对象属性赋值c.提供当前枚举类的多个对象:public static final......
  • 数据结构与算法【Java】08---树结构的实际应用
    前言数据data结构(structure)是一门研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码。要学习好数据结构就......
  • java多线程--3 线程状态、线程方法、线程类型
    java多线程--3线程状态、线程方法、线程类型线程状态创建状态:**就绪状态:**进入状态:创建状态:启动线程阻塞状态:阻塞解除运行状态:释放CPU资源阻塞状态:......
  • Java SE 宋红康 days03-高级篇-常用类
    1.String字符串String声明为final的,不可被继承;实现了Serializable接口:表示字符串是支持序列化的;实现了Comparable接口:表示String可以比较大小St......