public class ReadZipFile {
private static void mkdir(File file) {
if (!file.exists())
file.mkdir();
else if (!file.isDirectory()) {
throw new RuntimeException(String.format("因为%s已经存在,且不是目录,所以%<s目录无法创建", file.getPath()));
}
}
public static void main(String args[]) throws FileNotFoundException, IOException {
File contextpath = new File("mybook");
mkdir(contextpath);
byte b[] = new byte[8 * 1024];
try (ZipInputStream zipin = new ZipInputStream(ReadZipFile.class.getResourceAsStream("book.zip"),
Charset.forName("gbk"));) {
ZipEntry zipEntry = null;
while ((zipEntry = zipin.getNextEntry()) != null) {
File item = new File(contextpath, zipEntry.getName());
if (zipEntry.isDirectory()) {
System.out.println("创建了目录"+item.getAbsolutePath());
mkdir(item);
} else {
try (FileOutputStream out = new FileOutputStream(item);) {
int n = -1;
System.out.println("创建了文件"+item.getAbsolutePath());
while ((n = zipin.read(b, 0, b.length)) != -1) {
out.write(b, 0, n);
}
}
try (FileInputStream in = new FileInputStream(item);){
byte[] allbytes = new byte[(int) item.length()];
in.read(allbytes);
System.out.println(new String(allbytes));
}
}
}
}
}
}
大体功能是打开myBook目录下的book.zip文件,并解压出来
public class ReadZipFile {//是一个名为ReadZipFile(读压缩文件)的公有类
private static void mkdir(File file) {//私有成员函数mkdir用来创建目录的
if (!file.exists())//如果该文件不存在 exists()函数判断文件是否存在
file.mkdir();//不存在同名文件就创建该名字的目录
else if (!file.isDirectory()) {//文件存在且不是目录 isDirectory()函数判断是否有这个目录
throw new RuntimeException(String.format("因为%s已经存在,且不是目录,所以%<s目录无法创建", file.getPath()));//不是目录,才会抛出这个异常
}
}
public static void main(String args[]) throws FileNotFoundException, IOException {//主函数,且可以抛出 FileNotFoundException, IOException这两类异常
File contextpath = new File("mybook");//创建一个名为mybook的File类型的变量
mkdir(contextpath); //打开该路径
byte b[] = new byte[8 * 1024];//该数组能存8*1024个以字节为单位的数据
try (ZipInputStream zipin = new ZipInputStream(ReadZipFile.class.getResourceAsStream("book.zip"),
Charset.forName("gbk"));) {//第一个参数ZipInputStream“压缩文件输入流”,用于读取磁盘上的压缩文件 //第二个参数创建GBK对应的字符集,GBK编码专门用来解决中文编码的
ZipEntry zipEntry = null;//java.util.zip.ZipInputStream.getNextEntry()方法读取下一个ZIP文件条目并将该流定位在条目数据的开头。
//getNextEntry()函数返回值下一个ZIP文件条目,如果没有更多条目,则返回null。
while ((zipEntry = zipin.getNextEntry()) != null) {//一直读取需要解压的zip文件里的条目,直到没有可读的
File item = new File(contextpath, zipEntry.getName());
if (zipEntry.isDirectory()) {//如果解压包里有目录
System.out.println("创建了目录"+item.getAbsolutePath());
mkdir(item);//那就创建目录
} else {//如果是个文件
try (FileOutputStream out = new FileOutputStream(item);) {
int n = -1;
System.out.println("创建了文件"+item.getAbsolutePath());
while ((n = zipin.read(b, 0, b.length)) != -1) {//就创建文件,把需要解压的文件里的数据(通过zipin这个变量)循环写入写到b数组中
out.write(b, 0, n);//把b数组中写入解压后新创建的文件中
}//这一段代码是想把需要解压的文件里的数据放到新建的文件里(作为解压后的文件)
}
try (FileInputStream in = new FileInputStream(item);){//这里的item和前面 File item = new File(contextpath, zipEntry.getName());要联系起来
byte[] allbytes = new byte[(int) item.length()];
in.read(allbytes);//把输入流in变量里的数据读入到allbytes这个数组中
System.out.println(new String(allbytes));
//这一段代码是想把文件里的数据打印输出到控制台上
}
}
}
}
}
}
知识点参考:UTF-8和GBK有什么区别? https://zhidao.baidu.com/question/133119038.html
IO流之ZipInputStream和ZipOutputStream的认识及常用使用 https://blog.csdn.net/weixin_39723544/article/details/80611810
java.util.zip.ZipInputStream.getNextEntry()方法示例 https://www.yiibai.com/javazip/javazip_zipinputstream_getnextentry.html
Java学习-IO流-read()和write()详解 https://blog.csdn.net/m0_37257670/article/details/78711220