社区
Web 开发
帖子详情
用java.util.zip包解压zip文件时中文文件名的问题
Dishine
2004-06-08 02:28:08
如题,用ZipEntry中的getName方法可以获得zip文件中的每个文件,如果是中文名的话就显示“???”,请问如何解决。
我是用winRAR做的压缩文件。
以下的方法我已试过,不行:
String fn = zipEntry.getName();
fn = new String(fn.getBytes("ISO-8859-1"), "gb2312");
...全文
123
1
打赏
收藏
用java.util.zip包解压zip文件时中文文件名的问题
如题,用ZipEntry中的getName方法可以获得zip文件中的每个文件,如果是中文名的话就显示“???”,请问如何解决。 我是用winRAR做的压缩文件。 以下的方法我已试过,不行: String fn = zipEntry.getName(); fn = new String(fn.getBytes("ISO-8859-1"), "gb2312");
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
yemao20
2004-06-08
打赏
举报
回复
up
用
java
.
util
.
zip
包
现数据压缩与
解压
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用
java
.
util
.
zip
包
实现数据的压缩与
解压
,并扩展到在网络传输方面如何应用
java
.
util
.
zip
包
现数据压缩与
解压
java
.
util
.
zip
解压
缩
文件
,
ZIP
格式压缩
文件
.rar
Java
解压
缩
文件
,并以
ZIP
格式压缩
文件
,主要是使用
java
.
util
.
zip
包
中的类来实现
解压
、压缩
文件
功能,如果你对这个类并不太熟悉,你正好可以参考一下这个类是如何用的。
根据
java
.
util
.
zip
源码修改
zip
支持
中文
java
支持
中文
源程序,根据
java
.
util
.
zip
源码修改
java
.
util
.
zip
不支持
中文
java
+apache完成
zip
压缩源码(
包
括修改后的
java
.
util
.
zip
下的源码)
Java
完成
zip
压缩源码,
包
括修改后的
java
.
util
.
zip
下的
文件
(可以解决
中文
文件
名
的
问题
)
org.apache.tools.
zip
解决
解压
乱码
问题
package com.cliff.common; import
java
.io.File; import
java
.io.FileInputStream; import
java
.io.FileNotFoundException; import
java
.io.FileOutputStream; import
java
.io.IOException; import
java
.io.InputStream; import
java
.
util
.Enumeration; import org.apache.tools.
zip
.
Zip
Entry; import org.apache.tools.
zip
.
Zip
File; import org.apache.tools.
zip
.
Zip
OutputStream; /** * * 类名:
Zip
Util
.
java
* 描述:压缩/
解压
缩
zip
包
处理类 * 创建者:XXX * 创建日期:2015年5月7日 - 下午1:35:02 * 版本: V0.1 * 修改者: * 修改日期: */ public class
Zip
Util
{ /** * * 功能描述:压缩
文件
* 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:35:18 * 版本: V0.1 * 修改者: * 修改日期: * @param directory 指定压缩
文件
路径 压缩到同目录 * @throws IOException * void */ public static void
zip
(String directory) throws FileNotFoundException, IOException {
zip
("", null, directory); } /** * * 功能描述:压缩
文件
* 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:36:03 * 版本: V0.1 * 修改者: * 修改日期: * @param
zip
FileName 压缩产生的
zip
包
文件
名
--带路径,如果为null或空则默认按
文件
名
生产压缩
文件
名
* @param relativePath 相对路径,默认为空 * @param directory
文件
或目录的绝对路径 * void */ public static void
zip
(String
zip
FileName, String relativePath, String directory) throws FileNotFoundException, IOException { String fileName =
zip
FileName; if (fileName == null || fileName.trim().equals("")) { File temp = new File(directory); if (temp.isDirectory()) { fileName = directory + ".
zip
"; } else { if (directory.indexOf(".") > 0) { fileName = directory.substring(0, directory.lastIndexOf("."))+ "
zip
"; } else { fileName = directory + ".
zip
"; } } }
Zip
OutputStream zos = new
Zip
OutputStream(new FileOutputStream(fileName)); try {
zip
(zos, relativePath, directory); } catch (IOException ex) { throw ex; } finally { if (null != zos) { zos.close(); } } } /** * * 功能描述:压缩
文件
* 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:37:55 * 版本: V0.1 * 修改者: * 修改日期: * @param zos 压缩输出流 * @param relativePath 相对路径 * @param absolutPath
文件
或
文件
夹绝对路径 * @throws IOException * void */ private static void
zip
(
Zip
OutputStream zos, String relativePath, String absolutPath) throws IOException { File file = new File(absolutPath); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { File tempFile = files[i]; if (tempFile.isDirectory()) { String newRelativePath = relativePath + tempFile.getName() + File.separator; create
Zip
Node(zos, newRelativePath);
zip
(zos, newRelativePath, tempFile.getPath()); } else {
zip
File(zos, tempFile, relativePath); } } } else {
zip
File(zos, file, relativePath); } } /** * * 功能描述:压缩
文件
* 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:38:46 * 版本: V0.1 * 修改者: * 修改日期: * @param zos 压缩输出流 * @param file
文件
对象 * @param relativePath 相对路径 * @throws IOException * void */ private static void
zip
File(
Zip
OutputStream zos, File file, String relativePath) throws IOException {
Zip
Entry entry = new
Zip
Entry(relativePath + file.getName()); zos.putNextEntry(entry); InputStream is = null; try { is = new FileInputStream(file); int BUFFERSIZE = 2 <= 0) { zos.write(buffer, 0, length); } zos.flush(); zos.closeEntry(); } catch (IOException ex) { throw ex; } finally { if (null != is) { is.close(); } } } /** * * 功能描述:创建目录 * 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:39:12 * 版本: V0.1 * 修改者: * 修改日期: * @param zos
zip
输出流 * @param relativePath 相对路径 * @throws IOException * void */ private static void create
Zip
Node(
Zip
OutputStream zos, String relativePath) throws IOException {
Zip
Entry
zip
Entry = new
Zip
Entry(relativePath); zos.putNextEntry(
zip
Entry); zos.closeEntry(); } /** * * 功能描述:
解压
缩
文件
* 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:39:32 * 版本: V0.1 * 修改者: * 修改日期: * @param
zip
FilePath
zip
文件
路径 * @param targetPath
解压
缩到的位置,如果为null或空字符串则默认
解压
缩到跟
zip
包
同目录跟
zip
包
同名的
文件
夹下 * void */ public static void un
zip
(String
zip
FilePath, String targetPath) throws IOException { InputStream is = null; FileOutputStream fileOut = null; File file = null;
Zip
File
zip
File = null; try {
zip
File = new
Zip
File(
zip
FilePath,"GBK"); String directoryPath = ""; if (null == targetPath || "".equals(targetPath)) { directoryPath =
zip
FilePath.substring(0,
zip
FilePath.lastIndexOf(".")); } else { directoryPath = targetPath; } for(Enumeration entries =
zip
File.getEntries(); entries.hasMoreElements();){
Zip
Entry entry = (
Zip
Entry)entries.nextElement(); file = new File(directoryPath+"/"+entry.getName()); if(entry.isDirectory()){ file.mkdirs(); }else{ //如果指定
文件
的目录不存在,则创建之. File parent = file.getParentFile(); if(!parent.exists()){ parent.mkdirs(); } is =
zip
File.getInputStream(entry); fileOut = new FileOutputStream(file); int readLen = 0; byte[] buffer = new byte[4096]; while ((readLen = is.read(buffer, 0, 4096)) >= 0) { fileOut.write(buffer, 0, readLen); } fileOut.close(); is.close(); } }
zip
File.close(); } catch (IOException ex) { throw ex; } finally { if(null !=
zip
File){
zip
File = null; } if (null != is) { is.close(); } if (null != fileOut) { fileOut.close(); } } } /** * * 功能描述:生产
文件
如果
文件
所在路径不存在则生成路径 * 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:41:04 * 版本: V0.1 * 修改者: * 修改日期: * @param fileName
文件
名
带路径 * @param isDirectory 是否为路径 * @return * File */ public static File buildFile(String fileName, boolean isDirectory) { File target = new File(fileName); if (isDirectory){ target.mkdirs(); } else { if (!target.getParentFile().exists()) { target.getParentFile().mkdirs(); target = new File(target.getAbsolutePath()); } } return target; } }
Web 开发
81,117
社区成员
341,739
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章