JAVA核心知識点--Zip圧縮工具類(二)(中国語の文字化けを解決する)
5890 ワード
Javaプロジェクトではフォルダの内容をZip圧縮する必要があります.インターネット上のコードを参考にして、中の問題を修復しました.例えば、中国語の文字化け、ディレクトリに異常はありません.
使用されているJarパケット:ant-*.*.jarとロゴ4 j-*.jar、プロジェクト例のCSDNダウンロードアドレス:
http://download.csdn.net/download/pengjunlee/10043115.
ZipUtilツールのソースコード
使用されているJarパケット:ant-*.*.jarとロゴ4 j-*.jar、プロジェクト例のCSDNダウンロードアドレス:
http://download.csdn.net/download/pengjunlee/10043115.
ZipUtilツールのソースコード
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
private static final Logger logger = Logger.getLogger(ZipUtil.class);
/**
*
*
* @param src
*
* @param dest
* ,
* @param encoding
*
* @throws IOException
*/
public static void zip(String src, String dest, String encoding) {
ZipOutputStream out = null;
if (isEmptyStr(src) || isEmptyStr(dest)) {
logger.error("invalid compress parameters...");
return;
}
if (isEmptyStr(encoding)) {
encoding = "UTF-8";
}
try {
File outFile = new File(dest);
File parentFile = outFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
out = new ZipOutputStream(outFile);
out.setEncoding(encoding);
File fileOrDirectory = new File(src);
if (fileOrDirectory.isFile()) {
zipFileOrDirectory(out, fileOrDirectory, "");
} else {
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// , curPaths
zipFileOrDirectory(out, entries[i], "");
}
}
} catch (IOException e) {
logger.error(e.getMessage());
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
private static boolean isEmptyStr(String str) {
return str == null || str.length() == 0;
}
/**
*
*
* @param out
*
* @param file
*
* @param curPath
* ,
* @throws IOException
*/
private static void zipFileOrDirectory(ZipOutputStream out, File file,
String curPath) {
FileInputStream in = null;
try {
if (!file.isDirectory()) {
//
byte[] buffer = new byte[1024 * 1024];
int len;
in = new FileInputStream(file);
ZipEntry entry = new ZipEntry(curPath + file.getName());
out.putNextEntry(entry);
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.closeEntry();
} else {
//
File[] entries = file.listFiles();
for (int i = 0; i < entries.length; i++) {
// , curPath
zipFileOrDirectory(out, entries[i],
curPath + file.getName() + "/");
}
}
} catch (IOException e) {
logger.error(e.getMessage());
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
/**
*
*
* @param zipFileName
*
* @param outputDirectory
*
* @throws IOException
*/
@SuppressWarnings({ "rawtypes" })
public static void unzip(String zipFileName, String outputDirectory,
String encoding) {
ZipFile zipFile = null;
if (isEmptyStr(encoding)) {
encoding = "UTF-8";
}
try {
zipFile = new ZipFile(zipFileName, encoding);
Enumeration zipEntries = zipFile.getEntries();
ZipEntry zipEntry = null;
File dest = new File(outputDirectory);
if (!dest.exists()) {
dest.mkdirs();
}
while (zipEntries.hasMoreElements()) {
zipEntry = (ZipEntry) zipEntries.nextElement();
String entryName = new String(zipEntry.getName().getBytes(
encoding), encoding);
InputStream in = null;
FileOutputStream out = null;
try {
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator
+ name);
f.mkdirs();
} else {
int index = entryName.lastIndexOf("\\");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
index = entryName.lastIndexOf("/");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
// f.createNewFile();
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
int len;
byte[] buffer = new byte[1024 * 1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}
} catch (IOException e) {
logger.error(" ...");
logger.error(e.getMessage());
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
} catch (IOException e) {
logger.error(" ...");
logger.error(e.getMessage());
} finally {
if (null != zipFile) {
try {
zipFile.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
public static void main(String[] args) {
ZipUtil.zip("D:\\sourceFolder", "D:\\targetFolder\\test.zip", "GBK");
ZipUtil.unzip("D:\\targetFolder\\test.zip", "D:\\test", "GBK");
}
}