第一篇写完下载ZIP文件,这次就是java实现解压ZIP
不带密码解压ZIP文件代码如下
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import static java.awt.Window.log;
@Slf4j
public class ZipUtil {
public static void main(String[] args) throws IOException {
String filename = "F:\\administrator\\Desktop\\corp.zip";
String path = "F:\\\\administrator\\\\Desktop\\\\jieya";
ZipUtil.unZip(filename, path);
}
public static void unZip(String sourceFilename, String targetDir) throws IOException {
unZip(new File(sourceFilename), targetDir);
}
public static void unZip(File sourceFile, String targetDir) throws IOException {
long start = System.currentTimeMillis();
if (!sourceFile.exists()) {
throw new FileNotFoundException("cannot find the file = " + sourceFile.getPath());
}
ZipFile zipFile = null;
try{
zipFile = new ZipFile(sourceFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.isDirectory()) {
String dirPath = targetDir + "/" + entry.getName();
createDirIfNotExist(dirPath);
} else {
File targetFile = new File(targetDir + "/" + entry.getName());
createFileIfNotExist(targetFile);
InputStream is = null;
FileOutputStream fos = null;
try {
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
}finally {
try{
fos.close();
}catch (Exception e){
log.warn("close FileOutputStream exception", e);
}
try{
is.close();
}catch (Exception e){
log.warn("close InputStream exception", e);
}
}
}
}
log.info("解压完成,耗时:" + (System.currentTimeMillis() - start) +" ms");
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
log.warn("close zipFile exception", e);
}
}
}
}
public static void createDirIfNotExist(String path){
File file = new File(path);
createDirIfNotExist(file);
}
public static void createDirIfNotExist(File file){
if(!file.exists()){
file.mkdirs();
}
}
public static void createFileIfNotExist(File file) throws IOException {
createParentDirIfNotExist(file);
file.createNewFile();
}
public static void createParentDirIfNotExist(File file){
createDirIfNotExist(file.getParentFile());
}
}
带密码隔离解压zip文件
package com.sinosoft.Interfaces.utils;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class UnZipUtils {
public void unZip(String source,String dest,String password){
try {
File zipFile = new File(source);
ZipFile zFile = new ZipFile(zipFile);
zFile.setFileNameCharset("UTF-8");
File destDir = new File(dest);
if (!destDir.exists()) {
destDir.mkdirs();
}
if (zFile.isEncrypted()) {
zFile.setPassword(password.toCharArray());
}
zFile.extractAll(dest);
List<net.lingala.zip4j.model.FileHeader> headerList = zFile.getFileHeaders();
List<File> extractedFileList = new ArrayList<File>();
for (FileHeader fileHeader : headerList) {
if (!fileHeader.isDirectory()) {
extractedFileList.add(new File(destDir, fileHeader.getFileName()));
}
}
File[] extractedFiles = new File[extractedFileList.size()];
extractedFileList.toArray(extractedFiles);
for (File f : extractedFileList) {
System.out.println(f.getAbsolutePath() + "文件解压成功!");
}
} catch (ZipException e) {
e.printStackTrace();
}
}
}