压缩流
# 压缩流和解压缩流
# 解压缩流
负责把压缩包中的文件和文件夹解压出来
- zis.read() 的流程
- 每次调用 zis.getNextEntry() 会给其内部属性 entry赋值
- 此时调用read()方法,读取的是entry的数据,读取到文件末尾,返回-1;如果是文件夹直接返回-1;
- 如果调用read()方法,读取完entry的数据,返回-1以后,entry 会置为空;
- 如果不调用read()方法,entry对象会一直延续直到读取下一个entry对象
/*
* 解压缩流
*
* */
public class ZipStreamDemo1 {
public static void main(String[] args) throws IOException {
//1.创建一个File表示要解压的压缩包
File src = new File("D:\\aaa.zip");
//2.创建一个File表示解压的目的地
File dest = new File("D:\\");
//调用方法
unzip(src,dest);
}
//定义一个方法用来解压
public static void unzip(File src,File dest) throws IOException {
//解压的本质:把压缩包里面的每一个文件或者文件夹读取出来,按照层级拷贝到目的地当中
//创建一个解压缩流用来读取压缩包中的数据
ZipInputStream zip = new ZipInputStream(new FileInputStream(src));
//要先获取到压缩包里面的每一个zipentry对象
//表示当前在压缩包中获取到的文件或者文件夹
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();
}
}
# 压缩流
负责压缩文件或者文件夹
# 压缩单个文件
public class ZipStreamDemo2 {
public static void main(String[] args) throws IOException {
/*
* 压缩流
* 需求:
* 把D:\\a.txt打包成一个压缩包
* */
//1.创建File对象表示要压缩的文件
File src = new File("D:\\a.txt");
//2.创建File对象表示压缩包的位置
File dest = new File("D:\\");
//3.调用方法用来压缩
toZip(src,dest);
}
/*
* 作用:压缩
* 参数一:表示要压缩的文件
* 参数二:表示压缩包的位置
* */
public static void toZip(File src,File dest) throws IOException {
//1.创建压缩流关联压缩包
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
//2.创建ZipEntry对象,表示压缩包里面的每一个文件和文件夹
//参数:压缩包里面的路径
ZipEntry entry = new ZipEntry("aaa\\bbb\\a.txt");
//3.把ZipEntry对象放到压缩包当中
zos.putNextEntry(entry);
//4.把src文件中的数据写到压缩包当中
FileInputStream fis = new FileInputStream(src);
int b;
while((b = fis.read()) != -1){
zos.write(b);
}
zos.closeEntry();
zos.close();
}
}
# 压缩整个文件夹--1
public class ZipStreamDemo3 {
public static void main(String[] args) throws IOException {
/*
* 压缩流
* 需求:
* 把D:\\aaa文件夹压缩成一个压缩包
* */
//1.创建File对象表示要压缩的文件夹
File src = new File("D:\\aaa");
//2.创建File对象表示压缩包放在哪里(压缩包的父级路径)
File destParent = src.getParentFile();//D:\\
//3.创建File对象表示压缩包的路径
File dest = new File(destParent,src.getName() + ".zip");
//4.创建压缩流关联压缩包
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
//5.获取src里面的每一个文件,变成ZipEntry对象,放入到压缩包当中
toZip(src,zos,src.getName());//aaa
//6.释放资源
zos.close();
}
/*
* 作用:获取src里面的每一个文件,变成ZipEntry对象,放入到压缩包当中
* 参数一:数据源
* 参数二:压缩流
* 参数三:压缩包内部的路径
* */
public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {
//1.进入src文件夹
File[] files = src.listFiles();
//2.遍历数组
for (File file : files) {
if(file.isFile()){
//3.判断-文件,变成ZipEntry对象,放入到压缩包当中
ZipEntry entry = new ZipEntry(name + "\\" + file.getName());//aaa\\no1\\a.txt
zos.putNextEntry(entry);
//读取文件中的数据,写到压缩包
FileInputStream fis = new FileInputStream(file);
int b;
while((b = fis.read()) != -1){
zos.write(b);
}
fis.close();
zos.closeEntry();
}else{
//4.判断-文件夹,递归
toZip(file,zos,name + "\\" + file.getName());
// no1 aaa \\ no1
}
}
}
}
# 压缩整个文件夹--2
/**
* @ClassName ZipStreamDemo1
* @Date 2023/3/6 20:04
* @Author diane
* @Description 压缩流 --- 将文件夹打包成压缩包
*
* @Version 1.0
*/
public class ZipStreamDemo3 {
public static void main(String[] args) throws IOException {
// 要压缩的文件夹
File src = new File("D:\\tmp\\zip1");
// 压缩成的 zip文件
File dest = new File("D:\\tmp\\zip.zip");
// 创建压缩流对象
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
// 调用方法
toZip(src, zos, src.getName() + "\\");
// 关闭流
zos.close();
}
/**
* 压缩整个文件夹
* @param src 待压缩的文件夹
* @param zos 压缩流
* @param name 压缩包里面的文件夹名称 带路径的 aaa\\
* @throws IOException
*/
public static void toZip(File src, ZipOutputStream zos, String name) throws IOException {
// 递归遍历文件夹
File[] files = src.listFiles();
if (files ** null) {
return;
}
for (File file : files) {
if (file.isFile()) {
// 是文件 就进行压缩
fileToZip(file, name + file.getName(), zos);
} else {
// 是文件夹,就递归调用 --- name 的调用 还涉及到了回溯的知识
toZip(file, zos, name + file.getName() + "\\");
}
}
}
/**
* 将单个文件 进行压缩
* @param src 要压缩的文件
* @param name 压缩包内部的 文件名称 带路径的 aaa\\1.txt
* @param zos 压缩流
* @throws IOException
*/
private static void fileToZip(File src, String name, ZipOutputStream zos) throws IOException {
// 得到待压缩的文件名
System.out.println(name);
// 创建 ZipEntry对象
ZipEntry zipEntry = new ZipEntry(name);
// 把 ZipEntry对象 放入 压缩包中
zos.putNextEntry(zipEntry);
// 读取待压缩文件;;并把读取到的数据 写入 ZipEntry 对象
FileInputStream fis = new FileInputStream(src);
byte[] bytes = new byte[10];
int len;
while ((len = fis.read(bytes)) != -1) {
zos.write(bytes, 0, len);
}
// 关闭流
fis.close();
zos.closeEntry();
}
}
上次更新: 2023 07 3
