Zip压缩文件

package com.gxnu.study.zip;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.junit.Test;

public class ZipEx {
@Test
public void testZip(){
try(
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("e:/min.zip"));
){
String s1 = "a/b/d/b.ser";
String s2 = "a/b/d/a.xml";
ZipEntry entry = new ZipEntry(s1);
ZipEntry entry2 = new ZipEntry(s2);
zos.putNextEntry(entry);
zos.putNextEntry(entry2);
byte arr[] = Files.readAllBytes(Paths.get("e:","b.ser"));
byte arr1[] = Files.readAllBytes(Paths.get("e:","a.xml"));
zos.write(arr);
zos.write(arr1);
zos.closeEntry();
}catch(Exception e){
e.printStackTrace();
}
}


@Test
public void testMulZip(){
try(
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("e:/java.zip"));
){
String str = "C:\\Users\\Jimin\\Desktop\\three\\three";
File file = new File(str);
File[] listFiles = file.listFiles(f->f.isFile());
// Stream.of(listFiles).forEach(System.out::println);

String strRe = ".*(Users\\\\.*)";//String strRe = ".*(Users/.*)";
Pattern pattern = Pattern.compile(strRe);

int i=0;
String entryName[] = new String[listFiles.length];

for(File temp:listFiles){

String sStr = temp.toString();
// String replace = sStr.replace('\\', '/');
Matcher matcher = pattern.matcher(sStr);
if(matcher.find()){
String group1 = matcher.group(1);
System.out.println(group1);
entryName[i] = group1;
}
i++;

}

for(i = 0;i<entryName.length;i++){
ZipEntry entry = new ZipEntry(entryName[i]);
zos.putNextEntry(entry);
byte arr[] = Files.readAllBytes(listFiles[i].toPath());
zos.write(arr);
}
zos.closeEntry();
}catch(Exception e){
e.printStackTrace();
}
}

}

转载于:https://www.cnblogs.com/jiminluo/p/9343637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值