Java压缩和解压缩zip文件
介绍
Java提供的java.util.zip包只支持zip和gzip。至于更多格式的压缩可以选择apache的Commons Compress。
参考:https://o7planning.org/en/10195/java-compression-and-decompression-tutorial
读取zip文件列表
package com.dylan.javacore.compress;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @Description: 列出压缩包中的文件结构
* @Author laoxu
* @Date 2019/8/5 10:01
**/
public class ListZipEntriesDemo {
public static void main(String[] args) {
String FILE_PATH="D:/test/data.zip";
ZipInputStream zipls = null;
try {
zipls = new ZipInputStream(new FileInputStream(FILE_PATH), Charset.forName("GBK"));
ZipEntry entry = null;
while ((entry=zipls.getNextEntry())!=null){
if(entry.isDirectory()){
System.out.print("Directory:");
}else{
System.out.print("File:");
}
System.out.println(entry.getName());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(zipls!=null){
try {
zipls.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
输出:
解压zip到指定目录
package com.dylan.javacore.compress;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @Description: 解压zip
* @Author laoxu
* @Date 2019/8/5 13:50
**/
public class UnZipDemo {
public static void main(String[] args) {
final String OUTPUT_FOLDER="d:/test/output";
String FILE_PATH="d:/test/data.zip";
// 判断文件夹是否存在
File folder = new File(OUTPUT_FOLDER);
if(!folder.exists()){
folder.mkdir();
}
// 创建buffer
byte[] buffer = new byte[1024];
ZipInputStream zipls = null;
try {
zipls = new ZipInputStream(new FileInputStream(FILE_PATH), Charset.forName("GBK"));
ZipEntry entry = null;
while ((entry=zipls.getNextEntry())!=null){
String entryName = entry.getName();
String outFileName = OUTPUT_FOLDER + File.separator + entryName;
System.out.println("Unzip: " + outFileName);
if(entry.isDirectory()){
new File(outFileName).mkdirs();
}else{
FileOutputStream fos = new FileOutputStream(outFileName);
int len;
while ((len = zipls.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipls.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
压缩文件夹
package com.dylan.javacore.compress;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @Description: 压缩文件夹
* @Author laoxu
* @Date 2019/8/5 15:25
**/
public class ZipDirectory {
public ZipDirectory() {
}
// A method to Compress a directory.
public void zipDirectory(File inputDir, File outputZipFile) {
// Create parent directory for the output file.
outputZipFile.getParentFile().mkdirs();
String inputDirPath = inputDir.getAbsolutePath();
byte[] buffer = new byte[1024];
FileOutputStream fileOs = null;
ZipOutputStream zipOs = null;
try {
List<File> allFiles = this.listChildFiles(inputDir);
// Create ZipOutputStream object to write to the zip file
fileOs = new FileOutputStream(outputZipFile);
//
zipOs = new ZipOutputStream(fileOs, Charset.forName("GBK"));
for (File file : allFiles) {
String filePath = file.getAbsolutePath();
System.out.println("Zipping " + filePath);
// entryName: là một đường dẫn tương đối.
String entryName = filePath.substring(inputDirPath.length() + 1);
ZipEntry ze = new ZipEntry(entryName);
// Put new entry into zip file.
zipOs.putNextEntry(ze);
// Read the file and write to ZipOutputStream.
FileInputStream fileIs = new FileInputStream(filePath);
int len;
while ((len = fileIs.read(buffer)) > 0) {
zipOs.write(buffer, 0, len);
}
fileIs.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuite(zipOs);
closeQuite(fileOs);
}
}
private void closeQuite(OutputStream out) {
try {
out.close();
} catch (Exception e) {
}
}
// This method returns the list of files,
// including the children, grandchildren files of the input folder.
private List<File> listChildFiles(File dir) throws IOException {
List<File> allFiles = new ArrayList<File>();
File[] childFiles = dir.listFiles();
for (File file : childFiles) {
if (file.isFile()) {
allFiles.add(file);
} else {
List<File> files = this.listChildFiles(file);
allFiles.addAll(files);
}
}
return allFiles;
}
public static void main(String[] args) {
ZipDirectory zipDir = new ZipDirectory();
File inputDir = new File("D:/test/output");
File outputZipFile = new File("D:/test/output/datas.zip");
zipDir.zipDirectory(inputDir, outputZipFile);
}
}
下一篇讲Common Compress