import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @program:
* @description: 文件解压缩工具类
* @author: Mr.superbeyone
* @create: 2018-10-23 14:04
**/
public class FileUtils {
/**
* 此方法将默认设置解压缩后文件的保存路径为zip文件所在路径
* 即解压缩到当前文件夹下
* @param zip zip文件位置
* @param charsetName 字符编码
*/
public static void unpack(String zip, String charsetName) {
unpack(new File(zip), charsetName);
}
/**
*
* @param zip zip文件位置
* @param outputDir 解压缩后文件保存路径
* @param charsetName 字符编码
*/
public static void unpack(String zip, String outputDir, String charsetName) {
unpack(new File(zip), new File(outputDir), charsetName);
}
/**
* 此方法将默认设置解压缩后文件的保存路径为zip文件所在路径
* 即解压缩到当前文件夹
* @param zip zip文件位置
* @param charsetName 字符编码
*/
public static void unpack(File zip, String charsetName) {
unpack(zip, null, charsetName);
}
/**
*
* @param zip zip文件位置
* @param outputDir 解压缩后文件保存路径
*/
public static void unpack(File zip, File outputDir) {
unpack(zip, outputDir, "");
}
/**
*
* @param zip zip文件位置
* @param outputDir 解压缩后文件保存路径
* @param charsetName 字符编码
*/
public static void unpack(File zip, File outputDir, String charsetName) {
FileOutputStream out = null;
InputStream in = null;
//读出文件数据
ZipFile zipFileData = null;
ZipFile zipFile = null;
try {
//若目标保存文件位置不存在
if (outputDir != null) if (!outputDir.exists()) {
outputDir.mkdirs();
}
if (charsetName != null && charsetName != "") {
zipFile = new ZipFile(zip.getPath(), Charset.forName(charsetName));
} else {
zipFile = new ZipFile(zip.getPath(), Charset.forName("utf8"));
}
//zipFile = new ZipFile(zip.getPath(), Charset.forName(charsetName));
Enumeration extends ZipEntry> entries = zipFile.entries();
//处理创建文件夹
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String filePath = "";
if (outputDir == null) {
filePath = zip.getParentFile().getPath() + File.separator + entry.getName();
} else {
filePath = outputDir.getPath() + File.separator + entry.getName();
}
File file = new File(filePath);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
if (parentFile.isDirectory()) {
continue;
}
}
if (charsetName != null && charsetName != "") {
zipFileData = new ZipFile(zip.getPath(), Charset.forName(charsetName));
} else {
zipFileData = new ZipFile(zip.getPath(), Charset.forName("utf8"));
}
Enumeration extends ZipEntry> entriesData = zipFileData.entries();
while (entriesData.hasMoreElements()) {
ZipEntry entry = entriesData.nextElement();
in = zipFile.getInputStream(entry);
String filePath = "";
if (outputDir == null) {
filePath = zip.getParentFile().getPath() + File.separator + entry.getName();
} else {
filePath = outputDir.getPath() + File.separator + entry.getName();
}
File file = new File(filePath);
if (file.isDirectory()) {
continue;
}
out = new FileOutputStream(filePath);
int len = -1;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
zipFile.close();
zipFileData.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
/*File zip=new File("D:\\3D201932815242.zip");
String charsetName="UTF-8";
unpack(zip,charsetName);*/
File file=new File("D:\\3D201932815242\\pic");
File[] files = file.listFiles();
for(File f:files){
if(f.isFile()){
System.out.println(f.getName());
}
}
}
}