轉載https://blog.csdn.net/weixin_39723544/article/details/80611810java
import java.io.*;
import java.util.zip.*;
public class ZipUtils {
/**
* Default buff byte size
*
*/
private static final int DEFAULT_BUFF_SIZE = 1024;
/**
* Default basedir value
*
*/
private static final boolean DEFAULT_DIR = false;
public static void decompress(String srcPath) throws Exception {
decompress(new File(srcPath));
}
public static void decompress(File srcFile) throws Exception {
File baseFile = srcFile.getParentFile();
decompress(srcFile, baseFile);
}
public static void decompress(String srcPath, String destPath) throws Exception {
decompress(new File(srcPath), new File(destPath));
}
public static void decompress(File srcFile, File destFile) throws Exception {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
doDecompress(destFile, zis);
zis.close();
}
private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String dir = destFile.getPath() + File.separator + zipEntry.getName();
File dirFile = new File(dir);
fileProber(dirFile);
if (zipEntry.isDirectory()) {
dirFile.mkdirs();
} else {
doDecompressFile(dirFile, zis);
}
zis.closeEntry();
}
}
private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
}
/**
* 文件探測
*
* When the parent file not exist.Create it.
*
* @param dirFile
* @throws Exception
*/
public static void fileProber(File dirFile) throws Exception {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
parentFile.mkdirs();
}
}
public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {
compress(new File(srcPath), new File(destPath), dirFlag);
}
public static void compress(String srcPath, String destPath) throws Exception {
compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
}
public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
}
public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
if (srcFile.isDirectory()) {
if (dirFlag) {
doCompress(zos, srcFile, srcFile.getName() + File.separator);
} else {
doCompress(zos, srcFile, "");
}
} else {
doCompress(zos, srcFile, "");
}
zos.close();
}
public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
InputStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
}
}
壓縮
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class condense {
private static final boolean DEFAULT_DIR = true;
private static final int DEFAULT_BUFF_SIZE = 1024;
// 壓縮入口1
public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {
compress(new File(srcPath), new File(destPath), dirFlag);
}
// 壓縮入口2
public static void compress(String srcPath, String destPath) throws Exception {
compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
}
// 壓縮入口3
public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
}
public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
// 須要解壓的壓縮文件對象
// 壓縮輸出流
// 是否在壓縮文件時建立一個父文件夾後再壓縮
if (srcFile.isDirectory()) {
if (dirFlag) {
doCompress(zos, srcFile, srcFile.getName() + File.separator);
} else {
doCompress(zos, srcFile, "");
}
} else {
doCompress(zos, srcFile, "");
}
zos.close();
}
public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
// 遞歸循環,只壓縮其中全部文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
// 進行文件壓縮的操做
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
InputStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
}
解壓
import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.CRC32;import java.util.zip.CheckedInputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class decompress { /** * Default buff byte size */ private static final int DEFAULT_BUFF_SIZE = 1024; // 程序入口1 public static void decompress(String srcPath) throws Exception { decompress(new File(srcPath)); } // 程序入口2 public static void decompress(File srcFile) throws Exception { File baseFile = srcFile.getParentFile(); decompress(srcFile, baseFile); } // 程序入口3 public static void decompress(String srcPath, String destPath) throws Exception { decompress(new File(srcPath), new File(destPath)); } // 程序基本入口 public static void decompress(File srcFile, File destFile) throws Exception { CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32()); ZipInputStream zis = new ZipInputStream(cis); // 解壓操做 doDecompress(destFile, zis); zis.close(); } private static void doDecompress(File destFile, ZipInputStream zis) throws Exception { ZipEntry zipEntry = null; while ((zipEntry = zis.getNextEntry()) != null) { String dir = destFile.getPath() + File.separator + zipEntry.getName(); File dirFile = new File(dir); // 若是父文件夾不存在,則遞歸建立其父文件夾 fileProbe(dirFile); if (zipEntry.isDirectory()) { // 若是zipEntry是目錄,則建立目錄 dirFile.mkdirs(); } else { // 解壓壓縮文件的其中具體的一個zipEntry對象 doDecompressFile(dirFile, zis); } zis.closeEntry(); } } // 通常意義上的文件複製操做 private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); int len; byte[] buff = new byte[DEFAULT_BUFF_SIZE]; while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) { bos.write(buff, 0, len); } bos.close(); } /** * 文件探測 * * When the parent file not exist.Create it. * * @param dirFile * @throws Exception */ public static void fileProbe(File dirFile) throws Exception { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { fileProbe(parentFile); parentFile.mkdirs(); } }}