2023年11月29日发(作者:)

Java操作Zip

代码如下(不需要引⼊第三⽅包)

package ;

// java 操作Zip

import .*;

import ist;

import ation;

import ;

import ry;

import e;

import putStream;

public class Java4Zip {

private static final int BUFFER_SIZE = 2 * 1024;

/**

* 压缩成ZIP ⽅法1

* @param srcDir 压缩⽂件夹路径

* @param out 压缩⽂件输出流

* @param KeepDirStructure 是否保留原来的⽬录结构,true:保留⽬录结构;

* false:所有⽂件跑到压缩包根⽬录下(注意:不保留⽬录结构可能会出现同名⽂件,会压缩失败)

* @throws RuntimeException 压缩失败会抛出运⾏时异常

*/

public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)

throws RuntimeException{

long start = tTimeMillis();

ZipOutputStream zos = null ;

try {

zos = new ZipOutputStream(out);

File sourceFile = new File(srcDir);

compress(sourceFile,zos,e(),KeepDirStructure);

long end = tTimeMillis();

n("压缩完成,耗时:" + (end - start) +" ms");

} catch (Exception e) {

throw new RuntimeException("zip error from ZipUtils",e);

}finally{

if(zos != null){

try {

();

} catch (IOException e) {

tackTrace();

}

}

}

}

/**

* 压缩成ZIP ⽅法2

* @param srcFiles 需要压缩的⽂件列表

* @param out 压缩⽂件输出流

* @throws RuntimeException 压缩失败会抛出运⾏时异常

*/

public static void toZip(List srcFiles , OutputStream out)throws RuntimeException {

long start = tTimeMillis();

ZipOutputStream zos = null ;

try {

zos = new ZipOutputStream(out);

for (File srcFile : srcFiles) {

byte[] buf = new byte[BUFFER_SIZE];

tEntry(new ZipEntry(e()));

int len;

FileInputStream in = new FileInputStream(srcFile);

while ((len = (buf)) != -1){

(buf, 0, len);

}

ntry();

();

}

long end = tTimeMillis();

n("压缩完成,耗时:" + (end - start) +" ms");

} catch (Exception e) {

throw new RuntimeException("zip error from ZipUtils",e);

}finally{

if(zos != null){

try {

();

} catch (IOException e) {

tackTrace();

}

}

}

}

/**

* 递归压缩⽅法

* @param sourceFile 源⽂件

* @param zos zip输出流

* @param name 压缩后的名称

* @param KeepDirStructure 是否保留原来的⽬录结构,true:保留⽬录结构;

* false:所有⽂件跑到压缩包根⽬录下(注意:不保留⽬录结构可能会出现同名⽂件,会压缩失败)

* @throws Exception

*/

private static void compress(File sourceFile, ZipOutputStream zos, String name,

boolean KeepDirStructure) throws Exception{

byte[] buf = new byte[BUFFER_SIZE];

if(()){

// zip输出流中添加⼀个zip实体,构造器中namezip实体的⽂件的名字

tEntry(new ZipEntry(name));

// copy⽂件到zip输出流中

int len;

FileInputStream in = new FileInputStream(sourceFile);

while ((len = (buf)) != -1){

(buf, 0, len);

}

// Complete the entry

ntry();

();

} else {

File[] listFiles = les();

if(listFiles == null || == 0){

// 需要保留原来的⽂件结构时,需要对空⽂件夹进⾏处理

if(KeepDirStructure){

// 空⽂件夹的处理

tEntry(new ZipEntry(name + "/"));

// 没有⽂件,不需要⽂件的copy

ntry();

}

}else {

for (File file : listFiles) {

// 判断是否需要保留原来的⽂件结构

if (KeepDirStructure) {

// 注意:e()前⾯需要带上⽗⽂件夹的名字加⼀斜杠,

// 不然最后压缩包中就不能保留原来的⽂件结构,即:所有⽂件都跑到压缩包根⽬录下了

compress(file, zos, name + "/" + e(),KeepDirStructure);

} else {

compress(file, zos, e(),KeepDirStructure);

}

}

}

}

}

/**

* zip解压

* @param srcFile zip源⽂件

* @param destDirPath 解压后的⽬标⽂件夹

* @throws RuntimeException 解压失败会抛出运⾏时异常

*/

public static void unZip(File srcFile, String destDirPath) throws RuntimeException {

long start = tTimeMillis();

// 判断源⽂件是否存在

if (!()) {

throw new RuntimeException(h() + "所指⽂件不存在");

}

// 开始解压

ZipFile zipFile = null;

try {

zipFile = new ZipFile(srcFile);

Enumeration entries = s();

while (eElements()) {

ZipEntry entry = (ZipEntry) ement();

n("解压" + e());

// 如果是⽂件夹,就创建个⽂件夹

if (ctory()) {

String dirPath = destDirPath + "/" + e();

File dir = new File(dirPath);

();

} else {

// 如果是⽂件,就先创建⼀个⽂件,然后⽤io流把内容copy过去

File targetFile = new File(destDirPath + "/" + e());

// 保证这个⽂件的⽗⽂件夹必须要存在

if (!entFile().exists()) {

entFile().mkdirs();

}

NewFile();

// 将压缩⽂件内容写⼊到这个⽂件中

InputStream is = utStream(entry);

FileOutputStream fos = new FileOutputStream(targetFile);

int len;

byte[] buf = new byte[BUFFER_SIZE];

while ((len = (buf)) != -1) {

(buf, 0, len);

}

// 关流顺序,先打开的后关闭

();

();

}

}

long end = tTimeMillis();

n("解压完成,耗时:" + (end - start) + " ms");

} catch (Exception e) {

throw new RuntimeException("unzip error from ZipUtils", e);

} finally {

if (zipFile != null) {

try {

();

} catch (IOException e) {

tackTrace();

}

}

}

}

public static void main(String[] args) throws Exception {

/** 测试压缩⽅法1 */

FileOutputStream fos1 = new FileOutputStream(new File("/Users/wanghao/Documents/"));

("/Users/wanghao/other/test/测试", fos1,true);

/** 测试压缩⽅法2 */

List fileList = new ArrayList<>();

(new File("/Users/wanghao/other/test/接⼝机测试报⽂/杨超越深夜⾃拍变⾝⼩恶魔.xml"));

(new File("/Users/wanghao/other/test/接⼝机测试报⽂/我在北京.xml"));

FileOutputStream fos2 = new FileOutputStream(new File("/Users/wanghao/Documents/"));

(fileList, fos2);

(new File("/Users/wanghao/Documents/"),"/Users/wanghao/Documents/mytest01");

}

}

经测试可以满⾜使⽤。

参考代码:

压缩ZIP

unzip