要在webapp上做个文件解压,功能就是 前端发送解压请求,参数: 文件路径,文件名。后台找到对于文件,解压即可,但被中文乱码问题折腾了很久。
我用的是 org.apache.tools.zip 实现解压。
请求的编码是 UTF-8,webapp的编码是:GBK,
响应程序中用 new String(filename.getBytes(),"UTF-8"); 对参数处理后,无乱码,而且在测试机(linux)解压问题
但在正式机(AIX)当压缩文件为中文时,却找不到解压问题。而程序建立的文件,中文名全部乱码。
觉得是AIX系统文件名编码不是GBK所造成,所以将 解压部分独立成一java程序,通过响应程序通过
String java_cmd = “java -Dfile.encoding=UTF-8 UnzipFile 文件名 文件路径 ”
Process p =Runtime.run(java_cmd);
UnzipFile 是解压程序,经测试,可成功解压,但是调用的时候,参数无聊用什么编码,总出现乱码,
谁知道什么原因,或者有什么解决办法? 谢谢!
我用的是 org.apache.tools.zip 实现解压。
请求的编码是 UTF-8,webapp的编码是:GBK,
响应程序中用 new String(filename.getBytes(),"UTF-8"); 对参数处理后,无乱码,而且在测试机(linux)解压问题
但在正式机(AIX)当压缩文件为中文时,却找不到解压问题。而程序建立的文件,中文名全部乱码。
觉得是AIX系统文件名编码不是GBK所造成,所以将 解压部分独立成一java程序,通过响应程序通过
String java_cmd = “java -Dfile.encoding=UTF-8 UnzipFile 文件名 文件路径 ”
Process p =Runtime.run(java_cmd);
UnzipFile 是解压程序,经测试,可成功解压,但是调用的时候,参数无聊用什么编码,总出现乱码,
谁知道什么原因,或者有什么解决办法? 谢谢!
我以前对文件压缩都是用下面的方法
对文件进行压缩和解压的。
贴出来分享下/***
* 压缩GZip
*
* @param data
* @return
*/
public static byte[] gZip(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data);
gzip.finish();
gzip.close();
b = bos.toByteArray();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
} /***
* 解压GZip
*
* @param data
* @return
*/
public static byte[] unGZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
gzip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
} /***
* 压缩Zip
*
* @param data
* @return
*/
public static byte[] zip(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry("zip");
entry.setSize(data.length);
zip.putNextEntry(entry);
zip.write(data);
zip.closeEntry();
zip.close();
b = bos.toByteArray();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
} /***
* 解压Zip
*
* @param data
* @return
*/ public static byte[] unZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ZipInputStream zip = new ZipInputStream(bis);
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
} /***
* 压缩BZip2
*
* @param data
* @return
*/
public static byte[] bZip2(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);
bzip2.write(data);
bzip2.flush();
bzip2.close();
b = bos.toByteArray();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
} /***
* 解压BZip2
*
* @param data
* @return
*/
public static byte[] unBZip2(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
CBZip2InputStream bzip2 = new CBZip2InputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
bzip2.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}