package com.gblfy.test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* java實作zip壓縮檔案(同一檔案夾下的多個檔案夾打成一個zip包)
*
* @author gblfy
* @date 2020-07-02
*/
public class ZipCompressor {
static final int BUFFER = 8192;
private File zipFile;
public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
/**
* 壓縮入口
* 适配:
* 1.壓縮檔案
* 2.壓縮檔案夾
*
* @param pathName 傳入一個或者多個檔案/檔案夾的絕對路徑 可變參數
*/
public void compress(String... pathName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
for (int i = 0; i < pathName.length; i++) {
//循環周遊傳入的檔案或者檔案夾的絕對路徑的 可變參數
compress(new File(pathName[i]), out, basedir);
}
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 判斷傳參類型:是目錄還是檔案
* <p>
* 1.如果是檔案,則調用壓縮檔案方法
* 2.如果是目錄,則調用壓縮目錄方法
* </p>
*
* @param file
* @param out
* @param basedir
*/
private void compress(File file, ZipOutputStream out, String basedir) {
if (file.isDirectory()) {
System.out.println("壓縮:" + basedir + file.getName());
//調用壓縮目錄方法
this.compressDirectory(file, out, basedir);
} else {
System.out.println("壓縮:" + basedir + file.getName());
//調用壓縮檔案方法
this.compressFile(file, out, basedir);
}
}
/**
* 壓縮一個目錄
*/
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists()) {
System.out.println("壓縮目錄不存在,請核實!");
return;
}
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 遞歸 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/**
* 壓縮一個檔案
*/
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
System.out.println("壓縮檔案不存在,請核實!");
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 壓縮指定檔案(檔案個數限定1個)
*
* @param srcPathName
*/
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists()) {
throw new RuntimeException(srcPathName + "不存在!");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("D:/resource.zip");
// String b = "D:\\1\\";
String b = "D:\\1.jpg";
zc.compress(b);
// zc.compress("D:\\1.jpg", "D:\\3.jpeg", "D:\\4.jpg", b);
}
}
package com.gblfy.test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* java實作zip壓縮檔案(同一檔案夾下的多個檔案夾打成一個zip包)
*
* @author gblfy
* @date 2020-07-02
*/
public class ZipCompressor2 {
static final int BUFFER = 8192;
/**
* 壓縮入口
* 适配:
* 1.壓縮檔案
* 2.壓縮檔案夾
*
* @param pathName 傳入一個或者多個檔案/檔案夾的絕對路徑 可變參數
*/
public void compress(String pathName, String zipFileName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFileName);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
//循環周遊傳入的檔案或者檔案夾的絕對路徑的 可變參數
compress(new File(pathName), out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 判斷傳參類型:是目錄還是檔案
* <p>
* 1.如果是檔案,則調用壓縮檔案方法
* 2.如果是目錄,則調用壓縮目錄方法
* </p>
*
* @param file
* @param out
* @param basedir
*/
private void compress(File file, ZipOutputStream out, String basedir) {
if (file.isDirectory()) {
System.out.println("壓縮:" + basedir + file.getName());
//調用壓縮目錄方法
this.compressDirectory(file, out, basedir);
} else {
System.out.println("壓縮:" + basedir + file.getName());
//調用壓縮檔案方法
this.compressFile(file, out, basedir);
}
}
/**
* 壓縮一個目錄
*/
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists()) {
System.out.println("壓縮目錄不存在,請核實!");
return;
}
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 遞歸 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/**
* 壓縮一個檔案
*/
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
System.out.println("壓縮檔案不存在,請核實!");
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 壓縮指定檔案(特殊場景)
* 檔案個數限定1個
*
* @param srcPathName
*/
public void compressFile(String srcPathName, String zipName) {
File file = new File(srcPathName);
if (!file.exists()) {
throw new RuntimeException(srcPathName + "不存在!");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipName);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
ZipCompressor2 zc = new ZipCompressor2();
String b = "D:\\1\\";
String zipName = "D:\\www.zip";
// String b = "D:\\1.jpg";
zc.compress(b, zipName);
// zc.compress("D:\\1.jpg", "D:\\3.jpeg", "D:\\4.jpg", b);
}
}