Java中压缩与解压--中文文件名乱码解决办法
----------------------------------------------------------------------
Apache Ant有个包专门处理ZIP文件,可以指定文件名的编码方式。由此可以解决问题。
例如:用ls.zip.ZipOutputStream代替java.util.zip.ZipOutputStream。
------------------------------------------------------------------------
java对於文字的编码是以 unicode为基础,因此,若是以ZipInputStream及ZipOutputStream来处理压缩及解压缩的工作,碰到中文档名或路径,那当然是以unicode来处理罗!但是,现在市面上的压缩及解压缩软体,例如winzip,却是不支援unicode的,一碰到档名以unicode编码的档案,它就不处理。 那要如何才能做出让WinRar能够处理的压缩档呢?那就得从修改ZipInputStream及ZipOutputStream对於档名的编码方式来着手了。我们可以从jdk的src.zip取得ZipInputStream及ZipOutputStream的原始码来加以修改
一、ZipOutputStream.java
1.从jdk的src.zip取得ZipOutputStream.java原始码,另存到一个新文件中,档名改为CnZipOutputStream.java。
2.开始修改原始码,将class名称改为CnZipOutputStream
3.建构式也必须更改为CnZipOutputStream
4.新增member,这个member记录编码方式
private String encoding="UTF-8";
5.再新增一个建构式(这个建构式可以让这个class在new的时候,设定档名的编码)
public CZipOutputStream(OutputStream out,String encoding) {
this(out);
}
6.到byte[] nameBytes = getUTF8Bytes(e.name);(有二个地方),将它修改如下:
byte[] nameBytes = null;
赵本山杀人被抓
try
{
if (UpperCase().equals("UTF-8"))
nameBytes =getUTF8Bytes(e.name);
else
nameBytes= ding);
}
catch(Exception byteE)
{
nameBytes=getUTF8Bytes(e.name);
}
二、ZipInputStream.java
1.从jdk的src.zip取得ZipInputStream.java原始码,另存到一个新文件中,档名改为CnZipInputStream.java。
2.开始修改原始码,将class名称改为CnZipInputStream
3.建构式也必须更改为CnZipInputStream
4.新增member,这个member记录编码方式
private String encoding="UTF-8";
5.再新增一个建构式如下(这个建构式可以让这个class在new的时候,设定档名的编码)
public CZipInputStream(InputStream in,String encoding) {
this(in);
}
6.到ZipEntry e = createZipEntry(getUTF8String(b, 0, len));这一行,将它改成如下:
ZipEntry e=null;
try
{
if (UpperCase().equals("UTF-8"))
e=createZipEntry(getUTF8String(b, 0, len));
else
e=createZipEntry(new String(b,0,ding));
}
catch(Exception byteE)
{
e=createZipEntry(getUTF8String(b, 0, len));
}
以上两个档案储存後compile产生CZipOutputStream.class及CZipInputStream.class,使用winzip开启 [java_home]\jre\lib\rt.jar这
个档案,将CnZipOutputStream.class及 CnZipInputStream.class加进去,以後当压缩及解压缩时有中文档名及路径的问题时,就可以指定编码方式来处理了。
使用方法:
CnZipOutputStream zos=new CnZipOutputStream(OutputStream os,String encoding);
CnZipInputStream zins=new CnZipInputStream(InputStream ins,String encoding);
对于前面文章中文件打包下载中遇到的中文文件问题,解决手法是:
OutputStream os = OutputStream();
CnZipOutputStream zos = new CnZipOutputStream(os,"gbk"); //加上中文编码类型
Properties Properties();
Iterator it=properties.keySet().iterator();
while (it.hasNext())菊麟
{
String ().toString();
String Property(key);
System.out.println(key+"=="+value);鼓励人坚强的话
观看顺序}
print======================
java.runtime.name==Java(TM) SE Runtime Environment
sun.boot.library.path==E:\Java\jdk1.6.0_16\jre\bin
java.vm.version==14.2-b01
java.vm.vendor==Sun Microsystems Inc.
java.vendor.url==java.sun/
path.separator==;
java.vm.name==Java HotSpot(TM) Client VM
sun.java.launcher==SUN_STANDARD
sun.os.patch.level==
java.vm.specification.name==Java Virtual Machine Specification
user.dir==E:\workspace\QuetzacoEIM
java.runtime.version==1.6.0_16-b01
aphicsenv==sun.awt.Win32GraphicsEnvironment
os.arch==x86
我的e家 路由器pdir==C:\Users\dong\AppData\Local\Temp\
line.separator==
java.vm.specification.vendor==Sun Microsystems Inc.企业年会主题口号
user.variant==
os.name==Windows 7
ding==GBK
java.library.path==E:\Java\jdk1.6.0_16\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program
Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;E:\Java\jdk1.6.0_16\;E:\Java\jdk1.6.0_16\bin\;E:\apache-ant-1.7.1\bin\;C:\Program Files\Windows Live\Shared
java.specification.name==Java Platform API Specification
java.class.version==50.0
sun.managementpiler==HotSpot Client Compiler
os.version==6.1
user.home==d:\Users\dong
user.timezone==
java.awt.printerjob==sun.awt.windows.WPrinterJob
java.specification.version==1.6
java.class.path==E:\workspace\QuetzacoEIM\bin;E:\workspace\QuetzacoEIM\lib\activation.jar;E:\workspace\QuetzacoEIM\lib\ant.jar;E:\workspace\QuetzacoEIM\lib\avalon-framework-4.1.3.jar;E:\workspace\QuetzacoEIM\lib\axis.jar;E:\workspace\QuetzacoEIM\lib\batik-all-1.6.jar;E:\workspace\QuetzacoEIM\lib\bcmail-jdk14-132.jar;E:\workspace\QuetzacoEIM\lib\bcprov-jdk14-132.jar;E:\workspace\QuetzacoEIM\lib\bjvip_quickaction.jar;E:\workspace\QuetzacoEIM\lib\c3p0.jar;E:\workspace\QuetzacoEIM\lib\cglib2.jar;E:\workspace\QuetzacoEIM\lib\checkstyle-all-4.2.jar;E:\workspace\QuetzacoEIM\lib\commons-beanutils.jar;E:\workspace\QuetzacoEIM\lib\commons-cli-1
.1.jar;E:\workspace\QuetzacoEIM\lib\commons-collections-3.1.jar;E:\workspace\QuetzacoEIM\lib\commons-collections.jar;E:\workspace\QuetzacoEIM\lib\commons-digester.jar;E:\workspace\QuetzacoEIM\lib\commons-discovery.jar;E:\workspace\QuetzacoEIM\lib\commons-fileupload-1.2.1.jar;E:\workspace\QuetzacoEIM\lib\commons-io-1.4.jar;E:\workspace\QuetzacoEIM\lib\commons-lang.jar;E:\workspace\QuetzacoEIM\lib\log4j.jar;E:\workspace\QuetzacoEIM\lib\commons-logging-api.jar;E:\workspace\QuetzacoEIM\lib\commons-logging.jar;E:\workspace\QuetzacoEIM\lib\cos.jar;E:
\workspace\QuetzacoEIM\lib\crimson.jar;E:\workspace\QuetzacoEIM\lib\dom4j-1.5.1.jar;E:\workspace\QuetzacoEIM\lib\dom4j.jar;E:\workspace\QuetzacoEIM\lib\ehcache.jar;E:\workspace\QuetzacoEIM\lib\FontBox-0.1.0-dev.jar;E:\workspace\QuetzacoEIM\lib\jai_codec.jar;E:\workspace\QuetzacoEIM\lib\jai_core.jar;E:\workspace\QuetzacoEIM\lib\xmlgraphics-commons-1.2.jar;E:\workspace\QuetzacoEIM\lib\fop.jar;E:\workspace\QuetzacoEIM\lib\hibernate-2.1.1.jar;E:\workspace\QuetzacoEIM\lib\hibernate-tools.jar;E:\workspace\QuetzacoEIM\lib\htmlparser.jar;E:\workspace\QuetzacoEIM\lib\IKAnalyzer1.4.jar;E:\workspace\QuetzacoEIM\lib\itext-2.1.0.jar;E:\workspace\QuetzacoEIM\lib\iTextAsian.jar;E:\workspace\QuetzacoEIM\lib\jakarta-regexp-1.4.jar;E:\workspace\QuetzacoEIM\lib\james.jar;E:\workspace\QuetzacoEIM\lib\jasperreports-3.5.2.jar;E:\workspace\QuetzacoEIM\lib\jaudiotagger-2.0.1.jar;E:\workspace\QuetzacoEIM\lib\javaee.jar;E:\workspace\QuetzacoEIM\lib\jaxen-core.jar;E:\workspace\QuetzacoEIM\lib\jaxen-jdom.jar;E:\workspace\QuetzacoEIM\lib\jaxrpc.jar;E:\workspace\QuetzacoEIM\jar;E:\workspace\QuetzacoEIM\lib\jcommon-0.8.0.jar;E:\workspace\QuetzacoEIM\lib\jcrontab.jar;E:\workspace\QuetzacoEIM\lib\jdom-contrib.jar;E:\workspace\QuetzacoEIM\lib\jdom.jar;E:\workspace\QuetzacoEIM\lib\jfreechart-0.9.8.jar;E:\workspace\QuetzacoEIM\lib\juh-3.1.0.jar;E:\workspace\QuetzacoEIM\lib\jurt-3.1.0.jar;E:\
workspace\QuetzacoEIM\lib\ridl-3.1.0.jar;E:\workspace\QuetzacoEIM\lib\unoil-3.1.0.jar;E:\workspace\QuetzacoEIM\lib\json-20080701.jar;E:\workspace\QuetzacoEIM\lib\jodconverter-core-3.0-beta-3.jar;E:\workspace\QuetzacoEIM\lib\jpim-0.1.jar;E:\workspace\QuetzacoEIM\lib\jta.jar;E:\workspace\QuetzacoEIM\lib\jxl.jar;E:\workspace\QuetzacoEIM\lib\kehan.jar;E:\workspace\QuetzacoEIM\lib\lucene-analyzers-2.2.0.jar;E:\workspace\QuetzacoEIM\lib\lucene-core-2.2.0.jar;E:\workspace\QuetzacoEIM\lib\lucene-demos-2.2.0.jar;E:\workspace\QuetzacoEIM\lib\lucene-highlighter-2.2.0.jar;E:\workspace\QuetzacoEIM\lib\mail.jar;E:\workspace\QuetzacoEIM\lib\metadata-extractor-2.3.1.jar;E:\workspace\QuetzacoEIM\lib\mlibwrapper_jai.jar;E:\workspace\QuetzacoEIM\lib\Multivalent20040415.jar;E:\workspace\QuetzacoEIM\lib\odmg.jar;E:\workspace\QuetzacoEIM\lib\PDFBox-0.7.3.jar;E:\workspace\QuetzacoEIM\lib\poi-3.6-20091214.jar;E:\workspace\QuetzacoEIM\lib\poi-contrib-3.6-20091214.jar;E:\workspace\QuetzacoEIM\lib\poi-ooxml-3.6-20091214.jar;E:\workspace\QuetzacoEIM\lib\poi-ooxml-schema
s-3.6-20091214.jar;E:\workspace\QuetzacoEIM\lib\poi-scratchpad-3.6-20091214.jar;E:\workspace\QuetzacoEIM\lib\postgresql.jar;E:\workspace\QuetzacoEIM\lib\quetzacofileserver.jar;E:\workspace\QuetzacoEIM\lib\saaj.jar;E:\workspace\QuetzacoEIM\lib\sax2.jar;E:\workspace\QuetzacoEIM\lib\saxpath.jar;E:\workspace\QuetzacoEIM\lib\scheduler-plugin.jar;E:\workspace\QuetzacoEIM\lib\sequoia-driver.jar;E:\workspace\QuetzacoEIM\lib\servlet-ap
i.jar;E:\workspace\QuetzacoEIM\lib\slf4j-api-1.4.0.jar;E:\workspace\QuetzacoEIM\lib\slf4j-jdk14-1.4.0.jar;E:\workspace\QuetzacoEIM\lib\SMSendAPI.jar;E:\workspace\QuetzacoEIM\lib\soalet.jar;E:\workspace\QuetzacoEIM\lib\struts-1.1.jar;E:\workspace\QuetzacoEIM\lib\Tidy.jar;E:\workspace\QuetzacoEIM\lib\vcard4j.jar;E:\workspace\QuetzacoEIM\lib\velocity.jar;E:\workspace\QuetzacoEIM\lib\wsdl4j.jar;E:\workspace\QuetzacoEIM\lib\xercesImpl.jar;E:\workspace\QuetzacoEIM\lib\xml-apis.jar;E:\workspace\QuetzacoEIM\lib\xalan.jar;E:\workspace\QuetzacoEIM\lib\xerces.jar;E:\workspace\QuetzacoEIM\lib\xmlbeans-2.3.0.jar;E:\workspace\QuetzacoEIM\lib\xmlParserAPIs.jar;E:\workspace\QuetzacoEIM\lib\xpp3_min-1.1.3.4.I.jar;E:\workspace\QuetzacoEIM\lib\xstream-1.2.2.jar;E:\workspace\QuetzacoEIM\lib\Yozo_RMI.jar;E:\workspace\QuetzacoEIM\src\james\lib\activation.jar;E:\workspace\QuetzacoEIM\src\james\lib\avalon-framework-4.1.3.jar;E:\workspace\QuetzacoEIM\src\james\lib\commons-collections.jar;E:\workspace\QuetzacoEIM\src\james\lib\commons-dbcp-1.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\commons-net-1.0.0-dev.jar;E:\workspace\QuetzacoEIM\src\james\lib\commons-pool-1.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\cornerstone.jar;E:\workspace\QuetzacoEIM\src\james\lib\dnsjava-1.4.3.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-baxter-1.0a.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-cli-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-collections-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-concurrent-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-configuration-1.0.jar;E:\workspace\Quetza
coEIM\src\james\lib\excalibur-containerkit-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-datasource-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-extension-1.0a.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-i18n-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-instrument-0.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-io-1.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-logger-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-pool-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-thread-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-threadcontext-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\excalibur-util-1.0.jar;E:\workspace\QuetzacoEIM\src\james\lib\isorelax.jar;E:\workspace\QuetzacoEIM\src\james\lib\j2ee.jar;E:\workspace\QuetzacoEIM\src\james\lib\jakarta-oro-2.0.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\jboss-client.jar;E:\workspace\QuetzacoEIM\
src\james\lib\jboss-common-client.jar;E:\workspace\QuetzacoEIM\src\james\lib\jbossall-client.jar;E:\workspace\QuetzacoEIM\src\james\lib\jdbc2_0-stdext.jar;E:\workspace\QuetzacoEIM\src\james\lib\jing.jar;E:\workspace\QuetzacoEIM\src\james\lib\jnp-
client.jar;E:\workspace\QuetzacoEIM\src\james\lib\junit-3.7.jar;E:\workspace\QuetzacoEIM\src\james\lib\log4j.jar;E:\workspace\QuetzacoEIM\src\james\lib\logkit.jar;E:\workspace\QuetzacoEIM\src\james\lib\mail-1.3.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\PDFBox-0.6.6.jar;E:\workspace\QuetzacoEIM\src\james\lib\phoenix-bsh-commands.jar;E:\workspace\QuetzacoEIM\src\james\lib\phoenix-client.jar;E:\workspace\QuetzacoEIM\src\james\lib\servlet.jar;E:\workspace\QuetzacoEIM\src\james\lib\xml-apis.jar;E:\workspace\QuetzacoEIM\src\james\lib\xalan-2.3.1.jar;E:\workspace\QuetzacoEIM\src\james\lib\xercesimpl-2.0.2.jar;E:\workspace\QuetzacoEIM\lib\QuetzacoQueueManagement-1.0.jar;E:\workspace\QuetzacoEIM\lib\javacsv.jar
user.name==dong
java.vm.specification.version==1.0
java.home==E:\Java\jdk1.6.0_16\jre
sun.del==32
user.language==zh
java.specification.vendor==Sun Microsystems Inc.
java.vm.info==mixed mode, sharing
java.version==1.6.0_16
sun.boot.class.path==E:\Java\jdk1.6.0_16\jre\lib\resources.jar;E:\Java\jdk1.6.0_16\jre\lib\rt.jar;E:\Java\jdk1.6.0_16\jre\lib\sunrsasign.jar;E:\Java\jdk1.6.0_16\jre\lib\jsse.jar;E:\Java\jdk1.6.0_16\jre\lib\jce.jar;E:\Java\jdk1.6.0_16\jre\lib\charsets.jar;E:\Java\jdk1.6.0_16\jre\classes
java.vendor==Sun Microsystems Inc.
file.separator==\
java.vendor.url.bug==java.sun/i
sun.ding==UnicodeLittle
dian==little
sun.desktop==windows
sun.cpu.isalist==pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86