zip导出_zip解压
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class JdkZipUtils {
public static final int BUFFER_SIZE_DIFAULT = 128;
public static void makeZip(String[] inFilePaths, String zipFilePath)
throws Exception {
File[] inFiles = new File[inFilePaths.length];
for (int i = 0; i < inFilePaths.length; i++) {
inFiles[i] = new File(inFilePaths[i]);
}
makeZip(inFiles, zipFilePath);
}
/**
* 文件打包导出
*
* @param inFiles 导出文件
* @param zipFilePath 导出地址
* @throws Exception
*/
public static void makeZip(File[] inFiles, String zipFilePath)
throws Exception {
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFilePath)));
for (int i = 0; i < inFiles.length; i++) {
doZipFile(zipOut, inFiles[i], inFiles[i].getParent());
}
zipOut.flush();
zipOut.close();
}
private static void doZipFile(ZipOutputStream zipOut, File file,
String dirPath) throws FileNotFoundException, IOException {
if (file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
String zipName = file.getPath().substring(dirPath.length());
while (zipName.charAt(0) == '\\' || zipName.charAt(0) == '/') {
zipName = zipName.substring(1);
}
ZipEntry entry = new ZipEntry(zipName);
zipOut.putNextEntry(entry);
byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
int size;
while ((size = bis.read(buff, 0, buff.length)) != -1) {
zipOut.write(buff, 0, size);
}
zipOut.closeEntry();
bis.close();
} else {
File[] subFiles = file.listFiles();
for (File subFile : subFiles) {
doZipFile(zipOut, subFile, dirPath);
}
}
}
public static void unZip(String zipFilePath, String storePath)
throws IOException {
unZip(new File(zipFilePath), storePath);
}
/**
* 文件解压
*
* @param zipFile zip文件
* @param storePath 解压后存放位置
* @throws IOException
*/
public static void unZip(File zipFile, String storePath) throws IOException {
if (new File(storePath).exists()) {
new File(storePath).delete();
}
new File(storePath).mkdirs();
ZipFile zip = new ZipFile(zipFile);
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory()) {
// TODO
} else {
String zipEntryName = zipEntry.getName();
if (zipEntryName.indexOf(File.separator) > 0) {
String zipEntryDir = zipEntryName.substring(0, zipEntryName
.lastIndexOf(File.separator) + 1);
String unzipFileDir = storePath + File.separator
+ zipEntryDir;
File unzipFileDirFile = new File(unzipFileDir);
if (!unzipFileDirFile.exists()) {
unzipFileDirFile.mkdirs();
}
}
InputStream is = zip.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(new File(storePath
+ File.separator + zipEntryName));
byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
int size;
while ((size = is.read(buff)) > 0) {
fos.write(buff, 0, size);
}
fos.flush();
fos.close();
is.close();
}
}
}
}