解压 zip 文件
import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class ZipOperation {public static void main(String[] args) {ZipTool z = new ZipTool();try{z.unzipFile("./java.zip","./test/");}catch (IOException e){e.printStackTrace();}}}class ZipTool{public int unzipFile(String filePath,String desc) throws IOException {ZipInputStream zip = null;ZipEntry entry = null;OutputStream out = null;InputStream sourceFilePath = new FileInputStream(filePath);int flag = -1;try{zip = new ZipInputStream(sourceFilePath);while ((entry = zip.getNextEntry())!=null){String path = entry.getName();if (entry.isDirectory()){System.out.println("文件夹路径为: " + path);File descFilePath = new File(desc+path);if (!descFilePath.exists()){descFilePath.mkdirs();}}else{// 这里应该是 文件夹 + 文件名 才是写入的路径out = new FileOutputStream(desc+path);byte[] buff = new byte[1024];int len;// zip.read(buff)while((len = zip.read(buff)) > 0 ){out.write(buff,0,len);}}}flag = 1;}finally {zip.close();out.close();}return flag;}}
压缩文件
/** @Description: For zip file and unzip file and read zipfile.* @author: johnw;* @Created: 6/23/2020;*/import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;public class ZipOperation {public static void main(String[] args) {ZipTool z = new ZipTool();try{z.zipFile("src", "./test.zip");}catch (IOException e){e.printStackTrace();}}}class ZipTool{public int zipFile(String filePath, String zipFilePath) throws IOException{// 新建一个 ZIP 输出的管道ZipOutputStream zip = null;// 新建一个文件输出的管道OutputStream out = new FileOutputStream(zipFilePath);// 新建一个读取文件的管道,用于将文件写入 zip 包里面InputStream readZipFile = null;// 要读取文件,就要使用到文件路径String realFilePath;try{zip = new ZipOutputStream(out);File files = new File(filePath);// 列出所有的文件夹以及文件for(File file : files.listFiles()){realFilePath = filePath +"/"+ file.getName();zip.putNextEntry(new ZipEntry(realFilePath));// 开始读取readZipFile = new FileInputStream(realFilePath);int b;// 写入zip的操作while((b = readZipFile.read())!= -1){zip.write(b);}}}finally {zip.close();out.close();readZipFile.close();}return -1;}}
读取压缩文件
有些时候我们只需要读取,并不需要解压操作
/*
* @Description: For zip file and unzip file and read zipfile.
* @author: johnw;
* @Created: 6/23/2020;
*/
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipOperation {
public static void main(String[] args) {
ZipTool z = new ZipTool();
try{
//z.unzipFile("./java.zip","./test/");
//z.zipFile("src", "./test.zip");
z.readFile("./test.zip");
}catch (IOException e){
e.printStackTrace();
}
}
}
class ZipTool{
public void readFile(String filePath)throws IOException{
// 先准备一个 zip 管道
ZipInputStream zip = null;
// 再准备一个文件读取管道
InputStream in = null;
ZipEntry entry = null;
try{
// Zip 管道套上 文件读取管道, 就可以读取zip 文件信息了
in = new FileInputStream(filePath);
zip = new ZipInputStream(in);
// 循环遍历读取路径
while((entry = zip.getNextEntry()) != null) {
String fileName = entry.getName();
System.out.println(fileName);
}
}finally {
zip.close();
in.close();
}
}
}
Code: https://github.com/01x01/Java/blob/master/src/ZipOperation.java

