java zip压缩解压 工具类,zip压缩解压
分享于 点击 15833 次 点评:160
java zip压缩解压 工具类,zip压缩解压
/**
* 创建于:2015年11月16日 上午10:35:33
* 所属项目:
* 文件名称:ZipUtil.java
* 作者:test
* 版权信息:
*/
package zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void main(String[] args) {
unzip("E:/Exs.zip", "E:/Exstxt");
//zip("E:/Exs3.zip", "E:/申请.docx");
}
/**
* 解压zip文件
* @param needZipFilePath 需要解压的zip文件名
* @param unzipFolerPath 解压的目标路径
*/
public static void unzip(String needZipFilePath, String unzipFolerPath) {
ZipInputStream zipInputStream = null;
try {
File unzipFoler = new File(unzipFolerPath);
if (!unzipFoler.exists()) {
unzipFoler.mkdirs();
}
zipInputStream = new ZipInputStream(new FileInputStream(needZipFilePath));
ZipEntry zipEntry = null;
byte[] ch = new byte[1024];
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
File unzipFile = new File(unzipFoler, zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!unzipFile.exists()) {
unzipFile.mkdirs();
}
} else {
FileOutputStream fouts = null;
try {
fouts = new FileOutputStream(unzipFile);
int i;
while ((i = zipInputStream.read(ch)) != -1) {
fouts.write(ch, 0, i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fouts != null) {
try {
fouts.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
zipInputStream.closeEntry();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipInputStream != null) {
try {
zipInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩成zip文件
* @param zipFileName 压缩后的zip文件名
* @param inputFileName 待压缩的文件或者文件夹名
*/
public static void zip(String zipFileName, String inputFileName) {
zip(zipFileName, new File(inputFileName));
}
/**
* 压缩成zip文件
* @param zipFileName 压缩后的zip文件名
* @param inputFile 待压缩的文件
*/
public static void zip(String zipFileName, File inputFile) {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩成zip文件
* @param outputStream ZipOutputStream
* @param file 待压缩文件
* @param base 待压缩文件相对路径
* @throws IOException 操作outputStream时发生IO异常
*/
public static void zip(ZipOutputStream outputStream, File file, String base) throws IOException {
if (file.isDirectory()) {
File[] fl = file.listFiles();
if(base.length() > 0){
outputStream.putNextEntry(new ZipEntry(base + "/"));
}
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(outputStream, fl[i], base + fl[i].getName());
}
} else {
base = base.length() == 0 ? file.getName() : base;
outputStream.putNextEntry(new ZipEntry(base));
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
int b;
while ((b = inputStream.read()) != -1) {
outputStream.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
相关文章
- 暂无相关文章
用户点评