当前位置:网站首页>ZipOutputStream使用
ZipOutputStream使用
2022-06-10 11:57:00 【wfsm】

java提供的,类似于压缩软件的功能,可以解压缩文件
引用:https://www.jianshu.com/p/e9d01b3e82fc
https://blog.csdn.net/u010366748/article/details/78615004
public class Demo03 {
public static void compress(File f, String basedir, ZipOutputStream zos) throws IOException {
File[] fs = f.listFiles();
BufferedInputStream bis = null;
byte[] bytes = new byte[1024 * 10];
FileInputStream fis = null;
try {
for (int i = 0; i < fs.length; i++) {
String fName = fs[i].getName();
System.out.println("压缩" + basedir + fName);
if (fs[i].isFile()){
ZipEntry zipEntry = new ZipEntry(basedir + fName);
zos.putNextEntry(zipEntry);
fis = new FileInputStream(fs[i]);
bis = new BufferedInputStream(fis, 1024 * 10);
int read =0;
while ((read = bis.read(bytes,0,1024*10)) != -1){
zos.write(bytes,0,read);
}
}else if(fs[i].isDirectory()){
compress(fs[i],basedir+fName+"/",zos);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null){
bis.close();
}
if (fis != null){
fis.close();
}
}
}
public static void main(String[] args) throws IOException {
ZipOutputStream zos = null;
try {
String sourceFilePath = "E:\\test";
File sourceDir = new File(sourceFilePath);
File zipFile = new File(sourceFilePath+".zip");
zos = new ZipOutputStream(new FileOutputStream(zipFile));
String basedir = "test/";
compress(sourceDir,basedir,zos);
} catch (IOException e) {
e.printStackTrace();
} finally {
zos.close();
}
}
}
public class Client {
private static void compressFileToZip(String filePath,String zipFilePath) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
doCompress("",filePath,zos);
//
zos.finish();
}
}
private static void doCompress(String parentFilePath,String filePath,ZipOutputStream zos){
// 根据 path 创建 file
File sourceFile = new File(filePath);
if (!sourceFile.exists()){
return;
}
// 拼接路径
String zipEntryName = parentFilePath+"/"+sourceFile.getName();
if (parentFilePath.isEmpty()){
zipEntryName = sourceFile.getName();
}
if (sourceFile.isDirectory()){
File[] childrenFiles = sourceFile.listFiles();
if (Objects.isNull(childrenFiles)){
return;
}
for (File childFile : childrenFiles) {
// 传入绝对路径
doCompress(zipEntryName,childFile.getAbsolutePath(),zos);
}
}else{
byte[] buffer = new byte[1024];
try(BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile))){
zos.putNextEntry(new ZipEntry(zipEntryName));
int len;
while ((len = input.read(buffer)) != -1){
zos.write(buffer,0,len);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// compressFileToZip("e://test","e://test1.zip");
decompressFromZip("e://test1.zip","e:/hehe");
}
private static void decompressFromZip(String zipFilePath,String destFilePath) throws IOException {
File file = new File(zipFilePath);
ZipFile zipFile = new ZipFile(file);
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null){
if (zipEntry.getName().equals(file.getName())){
continue;
}
String fileName = destFilePath + "/"+zipEntry.getName();
File entryFile = new File(fileName);
if (zipEntry.isDirectory()){
// 是文件夹
entryFile.mkdir();
}else {
if (!entryFile.getParentFile().exists()){
// 父文件不存在,,必須保證父文件存在
entryFile.getParentFile().mkdirs();
}
entryFile.createNewFile();
}
try (
InputStream input = zipFile.getInputStream(zipEntry);
FileOutputStream output = new FileOutputStream(entryFile)) {
byte[] bytes = new byte[1024];
while (true){
int len = input.read(bytes);
if (len == -1){
break;
}
output.write(bytes,0,len);
}
}
}
}
}
}
边栏推荐
- Weekly 5.28
- 2022 CISCN 初赛pwn完整wp
- 2022 CISCN初赛 Satool
- 因为CircleImageView导致Binary XML file line #96: Error inflating class &lt;unknown&gt;
- 十、状态机模型的应用(细胞自动机;gdb/rr/perf;代码验证工具)
- web设计与开发,高效web开发
- TMUX use
- Peking University and Microsoft | progress in reasoning to make language models better
- How to insert PDF into ppt as vector graph
- "Yi +" open source program - Design and practice of low delay player based on webrtc
猜你喜欢

"Duty" is waiting for you| Book planning and editing (Conference Proceedings)

还活在上个时代,Etcd 3.0 实现分布式锁竟如此简单!

Amateurs don't ask for help, drag and drop for 30 seconds to make the cover image

Surrender firehouse database access: Twitter is ready to meet Musk's requirements

业余不求人,30秒拖拽制作封面图

Using Joern to process a large number of files and generate PDG is a slow solution

425万预算招标:江苏电信Oracle、TeleDB、TelePG等核心系统数据库驻场维保服务集中采购

Source of concurrent bugs (I) - visibility

聊聊消息中间件(1),AMQP那些事儿

入门篇:从 etcd 名字的由来,到安装部署和使用姿势
随机推荐
'getdrawable (int) 'is deprecated, getdrawable is obsolete
Is it safe to open an account in qiniu
【云图说】每个成功的业务系统都离不开APIG的保驾护航
Yet Another Palindrome Partitioning
线性代数的本质4 矩阵乘法与线性复合变换
证券期货业迎数据监管新规,IP-guard助力完善数据安全管理
mysql8.0安装
在启牛开户证券安全吗
【论文泛读】 知识蒸馏:Distilling the knowledge in a neural network
shell大全
还活在上个时代,Etcd 3.0 实现分布式锁竟如此简单!
Transfomer各组件与Pytorch
CLIP使用
[yuntu Said] chaque système d'affaires réussi ne peut pas être séparé de l'escorte de l'apig
交出 Firehouse 数据库访问权:推特准备满足马斯克要求
Transfomer self implementation and official library
安全工器具的使用和检查
绿盟数据库防火墙(DAS-FW)获得鲲鹏Validated认证
IDEA自定义配置链接nacos
Web development