Java基础 解压缩流、压缩流

解压缩流 就是读取压缩包中的文件,所以它是读,属于输入流
压缩流 就是把文件中的数据 写到压缩包中,所以它是写,属于输出流
压缩包 里面的 每一个文件或文件夹 在 Java 当中都是一个 ZipEntry 对象
所以 解压 的本质其实就是 把压缩包里面的每一个 ZipEntry 对象按照 层级结构 拷贝到本地另一个文件夹中
解压代码:
public static void main(String[] args) throws Exception {
//创建一个 File 表示要解压的压缩包
File src = new File("E:\\Java基础资料\\aaa.zip");
//创建一个 File 对象表示解压的目的地
File dest = new File("E:\\Java基础资料\\after_jieya");
unzip(src, dest);
}
public static void unzip(File src, File dest) throws Exception {//定义一个方法用来解压
//参数1:数据源,表示要解压的压缩包 参数2:解压的目的地
//创建一个解压缩流 用来读取压缩包中的数据
ZipInputStream zip = new ZipInputStream(new FileInputStream(src));
//要先获取到压缩包里面的每一个 zipentry 对象
while (true) {
ZipEntry entry = zip.getNextEntry();
if (entry == null)
break;
if (entry.isDirectory()) {
//文件夹:需要再在目的地dest里面创建一个同样的文件夹
File file = new File(dest, entry.toString());
file.mkdirs();
} else {
//文件:读取并存放到dest文件夹当中(按层级目录存放)
FileOutputStream fos = new FileOutputStream(new File(dest, entry.toString()));
while (true) {
int b = zip.read();
if (b == -1)break;
fos.write(b);
}
fos.close();
zip.closeEntry();//表示在压缩包中的一个文件处理完毕了
}
}
zip.close();
}
---------------------------------------------------------------------------------------------------------------------------
压缩流:
压缩就是把多个文件或多个文件夹变成一个压缩包,压缩包里面的每一个文件或文件夹都是一个 ZipEntry 对象
所以压缩的本质就 是把每一个文件或文件夹 看成 ZipEntry 对象放到压缩包中
一、把 一个文件 打包成一个压缩包
代码示例:
public static void main(String[] args) throws Exception {
//创建File对象表示要压缩的文件
File src = new File("E:\\Java基础资料\\a.txt");
//创建File对象表示压缩包的位置
File dest = new File("E:\\Java基础资料\\");
//调用方法,用来压缩
toZip(src, dest);
}
public static void toZip(File src, File dest) throws Exception {//参数1 表示要压缩的文件;参数2 表示压缩包的位置
//创建压缩流 关联压缩包
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);
while (true) {
int b = fis.read();
if (b == -1) break;
zos.write(b);
}
zos.closeEntry();
zos.close();
}
二、把一个文件夹 压缩成一个压缩包
代码示例:
public static void main(String[] args) throws Exception {
//创建File对象表示要压缩的文件夹
File src = new File("E:\\Java基础资料\\aaa");
//创建File对象表示压缩包放在那里(压缩包的父级路径)
File destParent = src.getParentFile();
//创建File对象表示压缩包的路径
File dest = new File(destParent, src.getName() + ".zip");
//创建压缩流 关联压缩包
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
//获取src里面的每一个文件,变成ZipEntry对象 放入压缩包当中
haha(src, zos, src.getName());
zos.close();
}
public static void haha(File src, ZipOutputStream zos, String name) throws Exception {
//方法的作用:获取src里面的每一个文件,变成ZipEntry对象 放入压缩包当中
//参数一:数据源 参数二:压缩流 参数三:压缩包内部的路径
//进入src文件夹
for (File file : src.listFiles()) {
if (file.isFile()) {
//文件,变成ZipEntry对象 放入压缩包当中
ZipEntry entry = new ZipEntry(name + "\\" + file.getName());
zos.putNextEntry(entry);
//读取文件中的数据,写到压缩包
FileInputStream fis = new FileInputStream(file);
while (true) {
int b = fis.read();
if (b == -1) break;
zos.write(b);
}
fis.close();
zos.closeEntry();
} else {
//文件夹,递归
haha(file, zos, name + "\\" + file.getName());
}
}
}
浙公网安备 33010602011771号