JAva圧縮---sun JDK圧縮を使う--中国語のファイル名は文字化けしになる
5730 ワード
テストの結果、ファイル名は中国語の圧縮が完了して文字化けして、テストしたことがある.txtのファイルタイプは、中国語の内容が正常に表示されます.
だからこの案は実行できない.
だからこの案は実行できない.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FileUtils;
/**
* @author ( ZIP ).
*/
public class ZipUtils {
/**
* . , .
*
* @param src
*
* @param zip
*
*/
public static void zip(File src, File zip) throws IOException {
zip(src, FileUtils.openOutputStream(zip));
}
/**
* .
*
* @param src
*
* @param out
*
*/
public static void zip(File src, OutputStream out) throws IOException {
zip(src, new ZipOutputStream(out));
}
/**
* ZIP .
*
* @param src
*
* @param zout
* ZIP
*/
public static void zip(File src, ZipOutputStream zout) throws IOException {
try {
doZip(src, zout, "");
} finally {
IOUtils.closeQuietly(zout);
}
}
/**
* . , .
*
* @param zip
*
* @param dest
*
*/
public static void unzip(File zip, File dest) throws IOException {
unzip(FileUtils.openInputStream(zip), dest);
}
/**
* .
*
* @param in
*
* @param dest
*
*/
public static void unzip(InputStream in, File dest) throws IOException {
unzip(new ZipInputStream(in), dest);
}
/**
* ZIP .
*
* @param zin
* ZIP
* @param dest
*
*/
public static void unzip(ZipInputStream zin, File dest) throws IOException {
try {
doUnzip(zin, dest);
} finally {
IOUtils.closeQuietly(zin);
}
}
/**
* @param src
* @param zout
* @param ns
*/
private static void doZip(File src, ZipOutputStream zout, String ns)
throws IOException {
for (File file : src.listFiles()) {
String entryName = ns + file.getName();
if (file.isDirectory()) {
System.out.println("adding: {}"+entryName + "/");
zout.putNextEntry(new ZipEntry(entryName));
doZip(file, zout, entryName);
} else {
System.out.println("adding: {}"+ entryName);
zout.putNextEntry(new ZipEntry(entryName));
fillZip(FileUtils.openInputStream(file), zout);
}
}
}
/**
* @param zin
* @param dest
*/
private static void doUnzip(ZipInputStream zin, File dest)
throws IOException {
for (ZipEntry e; (e = zin.getNextEntry()) != null; zin.closeEntry()) {
File file = new File(dest, e.getName());
if (e.isDirectory()) {
System.out.println(" creating: {}"+file.getName() + File.separator);
FileUtils.forceMkdir(file);
} else {
System.out.println("inflating: {}"+file);
flushZip(zin, FileUtils.openOutputStream(file));
}
}
}
/**
* @param in
* @param zout
*/
private static void fillZip(InputStream in, ZipOutputStream zout)
throws IOException {
try {
IOUtils.copy(in, zout);
} finally {
IOUtils.closeQuietly(in);
}
}
/**
* @param zin
* @param out
*/
private static void flushZip(ZipInputStream zin, OutputStream out)
throws IOException {
try {
IOUtils.copy(zin, out);
} finally {
IOUtils.closeQuietly(out);
}
}
/**
*
*/
private static Logger logger = LoggerFactory.getLogger(ZipUtils.class);
public static void main(String[] args){
try {
ZipUtils.zip(new File("D:\\evidence\\20120712\\ 1997_2012071213114144\\ _www.xs8.cn")
, new File("D:\\evidence\\20120712\\ 1997_2012071213114144\\ .zip"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}