JAVA压缩解压中文名乱码
来源:互联网 发布:php与php-fpm关系 编辑:程序博客网 时间:2024/05/01 18:54
sun提供的zip缺少对中文的支持,借助ant.jar实现对中文的处理!
package ant;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
* @file_name ZipAndUnzip
* @description 使用ant.jar实现压缩解压缩
* @author 张明亮
* @date 2009.8.30
*/
public class ZipAndUnzip {
/**
* @param zipFileName 指定压缩文件
* @param destDir 指定解压目录
* @throws Exception
*/
public static void unzip(String zipFileName, String destDir)
throws Exception {
try {
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration<?> e = zipFile.getEntries();
ZipEntry zipEntry = null;
File fD = new File(destDir);
if(!fD.exists()){
fD.mkdir();
}
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
String names[] = entryName.split("/");
int length = names.length;
String path = destDir;
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += "/" + names[v];
new File(path).mkdir();
} else {
if (entryName.endsWith("/")) {
new File(destDir + "/" + entryName).mkdir();
} else {
InputStream in = zipFile.getInputStream(zipEntry);
OutputStream os = new FileOutputStream(new File(
destDir + "/" + entryName));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
in.close();
os.close();
}
}
}
}
zipFile.close();
} catch (Exception ex) {
ex.printStackTrace();
}
// try {
// Project prj1 = new Project();
// Expand expand = new Expand();
// expand.setProject(prj1);
// expand.setSrc(new File("d:/tempsrc.zip"));
// expand.setOverwrite(false);
//
// File f = new File("d:/resultZip");
// expand.setDest(f);
// expand.execute();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
/**
* @param targetZip 目标ZIP
* @param sourceFile 源文件
*/
public static void zip(String targetZip, String sourceFile){
Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(new File(targetZip));
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(new File(sourceFile));
// fileSet.setIncludes("**/*.txt");
zip.addFileset(fileSet);
zip.execute();
}
public static void main(String[] args) {
// ============
try {
zip("d:/tempsrc.zip", "d:/ziptest");
unzip("d:/tempsrc.zip", "d:/resultZip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- JAVA压缩解压中文名乱码
- Linux下unzip解压windows压缩的zip件,中文名乱码问题
- ubuntu16 解压zip文件中文名乱码
- java 压缩解压文件 无乱码
- 解决linux,unzip 解压后中文名出现乱码现象
- Linux系统unzip解压后中文名乱码解决方法
- Linux系统unzip解压后中文名乱码解决方法
- Java中压缩与解压--中文文件名乱码解决办法
- Java中压缩与解压--中文文件名乱码解决办法
- Java实现Zip压缩与解压(解决中文乱码问题)
- Java中ZIP压缩与解压--中文文件名乱码解决办法
- java zip压缩 解压 乱码 解决(好用)
- 使用java实现压缩/解压(可以解决中文乱码)
- Java实现Zip压缩与解压(解决中文乱码问题)
- 下载文件中文名出现乱码 java
- java下载文件的时候中文名乱码
- Java文件下载中文名不显示&乱码
- java 压缩与解压
- Java经常会用的Jar包具体是些什么?
- ORACLE
- 计算机启动过程
- 物理考研复习
- 二级文法6-10
- JAVA压缩解压中文名乱码
- Hello csdn blog!
- 我是新来的
- Ant API
- 农村人比城市人更长寿
- char *、CString和string之间的类型转换
- sql server 海量数据速度提升:SQL优化-索引(1)
- sql server 海量数据速度提升:SQL优化-索引(2)
- sql server 海量数据速度提升:SQL优化-索引(3)