您的位置:首页 > 娱乐 > 八卦 > JavaSE 面向对象程序设计进阶 IO 压缩流 解压缩流

JavaSE 面向对象程序设计进阶 IO 压缩流 解压缩流

2024/10/6 18:24:23 来源:https://blog.csdn.net/qq_30500575/article/details/140373075  浏览:    关键词:JavaSE 面向对象程序设计进阶 IO 压缩流 解压缩流

目录

解压缩流

压缩流

解压缩流

压缩包

压缩包里面的每一个文件在java中都是一个ZipEntry对象

把每一个ZipEntry按照层级拷贝到另一个文件夹当中

import java.io.*;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class Main {public static void main(String[] args){//创建一个File表示要解压的压缩包File src=new File("D\\untitled.zip");//创建一个File表示解压的目的地File dest=new File("D\\");}public static void unzip(File src,File dest) throws IOException {//创建一个压缩流来读取压缩包中的数据ZipInputStream zip=new ZipInputStream(new FileInputStream(src));ZipEntry entry;while((entry=zip.getNextEntry())!=null){System.out.println(entry);if(entry.isDirectory()){//文件夹:需要在目的地dest处创建一个同样的文件夹File file=new File(dest,entry.toString());file.mkdirs();}else {//文件:需要读取压缩包中的文件,并且把它存放到目的地dest文件夹中FileOutputStream fos=new FileOutputStream(new File(dest,entry.toString()));int b;while((b=zip.read())!=-1){//写到目的地fos.write(b);}fos.close();//表示在压缩包中的一个文件处理完毕了zip.closeEntry();}}zip.close();}
}

压缩流

压缩单个文件

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class Main {public static void main(String[] args) throws IOException {//创建File对象表示要压缩的文件File src=new File("untitled\\a.txt");//创建File对象表示压缩包的位置File dest=new File("untitled\\");//调用方法用来压缩toZip(src,dest);}private static void toZip(File src, File dest) throws IOException {//常见压缩流关联压缩包ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));//创建ZipEntry对象,表示压缩包里面的每一个文件和文件夹ZipEntry entry=new ZipEntry("a.txt");//把ZipEntry对象放到压缩包中zos.putNextEntry(entry);//把src文件夹中的数据写到压缩包当中FileInputStream fis=new FileInputStream(src);int b;while((b=fis.read())!=-1)zos.write(b);zos.closeEntry();zos.close();}
}

压缩整个文件夹

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class Main {public static void main(String[] args) throws IOException {//创建File对象表示要压缩的文件夹File src=new File("untitled\\a");//创建File对象表示压缩包放在哪里(压缩包的父级路径)File destParent=src.getParentFile();//创建File对象表示压缩包的路径File dest=new File(destParent,src.getName()+".zip");//创建压缩流关联压缩包ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(dest));//获取src里面的每一个文件,变成ZipEntry对象,放到压缩包中//重点/** 参数一:数据源* 参数二:压缩流* 参数三:压缩包内部的路径* */toZip(src,zos,"");//释放资源zos.close();}public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {//进入src文件夹File []files=src.listFiles();//遍历数组for (File file : files) {if(file.isFile()){//判断是文件,变成ZipEntry对象,放入压缩包当中ZipEntry entry=new ZipEntry(name+"\\"+file.getName());zos.putNextEntry(entry);//读取文件中的数据,写到压缩包FileInputStream fis=new FileInputStream(file);int b;while((b=fis.read())!=-1){zos.write(b);}fis.close();zos.closeEntry();}else {//判断是文件夹,递归...toZip(file,zos,name+"\\"+file.getName());}}}
}

个人号推广

博客主页

朱道阳-CSDN博客

Web后端开发

https://blog.csdn.net/qq_30500575/category_12624592.html?spm=1001.2014.3001.5482

Web前端开发

https://blog.csdn.net/qq_30500575/category_12642989.html?spm=1001.2014.3001.5482

数据库开发

https://blog.csdn.net/qq_30500575/category_12651993.html?spm=1001.2014.3001.5482

项目实战

https://blog.csdn.net/qq_30500575/category_12699801.html?spm=1001.2014.3001.5482

算法与数据结构

https://blog.csdn.net/qq_30500575/category_12630954.html?spm=1001.2014.3001.5482

计算机基础

https://blog.csdn.net/qq_30500575/category_12701605.html?spm=1001.2014.3001.5482

回忆录

https://blog.csdn.net/qq_30500575/category_12620276.html?spm=1001.2014.3001.5482

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com