第一种使用ant实现的zip解压缩,其中解压的乱码注意使用public void unZip(String unZipFileName,String outputPath) 其中this.zipFile = new ZipFile(unZi
第一种使用ant实现的zip解压缩,其中解压的乱码注意使用
public void unZip(String unZipFileName,String outputPath) 其中
this.zipFile = new ZipFile(unZipFileName, "GB18030");是解决中文名乱码的关键。
import java.io.*;
import org.apache.tools.zip.*;
import java.util.Enumeration;
public class AntZip {
private ZipFile zipFile;
private ZipOutputStream zipOut; // 压缩Zip
private ZipEntry zipEntry;
private static int bufSize; // size of bytes
private byte[] buf;
private int readedBytes;
// 用于压缩中。要去除的绝对父路路径,目的是将绝对路径变成相对路径。
private String deleteAbsoluteParent;
public AntZip() {
this(512);
}
public AntZip(int bufSize) {
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
deleteAbsoluteParent = null;
}
public void doZip(String zipDirectory) {
File zipDir = new File(zipDirectory);
doZip(new File[] { zipDir }, zipDir.getName());
}
public void doZip(File[] files, String zipFileName) {
// 未指定压缩文件名,默认为"ZipFile"
if (zipFileName == null || zipFileName.equals(""))
zipFileName = "ZipFile";
// 添加".zip"后缀
if (!zipFileName.endsWith(".zip"))
zipFileName += ".zip";
try {
this.zipOut = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFileName)));
compressFiles(files, this.zipOut, true);
this.zipOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void compressFiles(File[] files, ZipOutputStream zipOut,
boolean isAbsolute) throws IOException {
for (File file : files) {
if (file == null)
continue; // 空的文件对象
// 删除绝对父路径
if (file.isAbsolute()) {
if (isAbsolute) {
deleteAbsoluteParent = file.getParentFile()
.getAbsolutePath();
deleteAbsoluteParent = appendSeparator(deleteAbsoluteParent);
}
} else
deleteAbsoluteParent = "";
if (file.isDirectory()) {// 是目录
compressFolder(file, zipOut);
} else {// 是文件
compressFile(file, zipOut);
}
}
}
public void compressFile(File file, ZipOutputStream zipOut)
throws IOException {
String fileName = file.toString();
if (file.isAbsolute())
fileName = fileName.substring(deleteAbsoluteParent.length());
if (fileName == null || fileName == "")
return;
if (file.isDirectory())
fileName = fileName + "/";// 此处不能用"\"
zipOut.putNextEntry(new ZipEntry(fileName));
// 如果是文件则需读;如果是空目录则无需读,直接转到zipOut.closeEntry()。
if (file.isFile()) {
FileInputStream fileIn = new FileInputStream(file);
while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
zipOut.write(this.buf, 0, this.readedBytes);
}
fileIn.close();
}
zipOut.closeEntry();
}
private void compressFolder(File dir, ZipOutputStream zipOut)
throws IOException {
File[] files = dir.listFiles();
if (files.length == 0)// 如果目录为空,则单独压缩空目录。
compressFile(dir, zipOut);
else
// 如果目录不为空,则分别处理目录和文件.
compressFiles(files, zipOut, false);
}
public void unZip(String unZipFileName) {
FileOutputStream fileOut;
File file;
InputStream inputStream;
try {
this.zipFile = new ZipFile(unZipFileName);
for (Enumeration entries = this.zipFile.getEntries(); entries
.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
file = new File(entry.getName());
if (entry.isDirectory()) {// 是目录,则创建之
file.mkdirs();
} else {// 是文件
// 如果指定文件的父目录不存在,则创建之.
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(file);
while ((this.readedBytes = inputStream.read(this.buf)) > 0) {
fileOut.write(this.buf, 0, this.readedBytes);
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void unZip(String unZipFileName,String outputPath) {
FileOutputStream fileOut;
File file;
InputStream inputStream;
try {
this.zipFile = new ZipFile(unZipFileName, "GB18030");
for (Enumeration entries = this.zipFile.getEntries(); entries
.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
file = new File(outputPath+entry.getName());
if (entry.isDirectory()) {// 是目录,则创建之
file.mkdirs();
} else {// 是文件
// 如果指定文件的父目录不存在,则创建之.
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(file);
while ((this.readedBytes = inputStream.read(this.buf)) > 0) {
fileOut.write(this.buf, 0, this.readedBytes);
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
} catch (ioException ioe) {
ioe.printStackTrace();
}
}
private String appendSeparator(String path) {
if (!path.endsWith(File.separator))
path += File.separator;
return path;
}
public void unZip(File unZipFile) {
unZip(unZipFile.toString());
}
public void setBufSize(int bufSize) {
this.bufSize = bufSize;
}
// 主函数,用于测试AntZip类
}
第二种 从修改ZipInputStream及ZipOutputStream对於档名的编码方式来着手了。
我们可以从jdk的src.zip取得ZipInputStream及ZipOutputStream的原始码来加以修改:
一、ZipOutputStream.java
从jdk的src.zip取得ZipOutputStream.java原始码,另存新档存到c:/java/util/zip这个资料夹里,档名改为CZipOutputStream.java。
开始修改原始码,将class名称改为CZipOutputStream
建构式也必须更改为CZipOutputStream
新增member,这个member记录编码方式
private String encoding="UTF-8";
再新增一个建构式(这个建构式可以让这个class在new的时候,设定档名的编码)
public CZipOutputStream(OutputStream out,String encoding) {
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
usesDefaultDeflater = true;
this.encoding=encoding;
}
--结束END--
本文标题: java中解压乱码解决方法
本文链接: https://lsjlt.com/news/1699.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0