当前位置:首页 » 编程语言 » javaunzip

javaunzip

发布时间: 2025-06-11 19:26:40

java用apache的ZipEntry压缩文件名为中文的word文件时,文件名乱码

apache自带的zip方法有缺陷,没有做中文的判断的,这个是它的一个已知bug。
解决办法:用jdk的rt.jar里面的方法实现就可以了。
可以参考下以下工具类:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;

/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}

/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}

/**
* 解压Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration<? extends ZipEntry> entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}

/**
* 将输入流中的数据写到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) > 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}

/**
* 安全关闭多个流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}

Ⅱ java中的ZipEntry是什么意思

ZipEntry类是java.util.zip包下的一个类,

ZipEntry类用于表示 ZIP 文件条目。

利用这个类压缩和解压zip文件

具体压缩的例子如下:

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;

/**
*压缩程序
*@authoryoung
*
*/
publicclassSingleFileZip{
publicstaticvoidmain(String[]args){
Filefile=newFile("e:/test.txt");
FileInputStreamfis=null;
ZipOutputStreamzos=null;
try{
fis=newFileInputStream(file);
zos=newZipOutputStream(newFileOutputStream("e:/my.zip"));

//创建压缩文件中的条目
ZipEntryentry=newZipEntry(file.getName());
//将创建好的条目加入到压缩文件中
zos.putNextEntry(entry);
//写入当前条目所对应的具体内容
byte[]buff=newbyte[1024];
intlen=0;
while((len=fis.read(buff))!=-1){
zos.write(buff,0,len);
}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
fis.close();
zos.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}

解压例子如下:

importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipInputStream;

/**
*解压程序
*@authoryoung
*
*/

publicclassSingleFileUnZip{
publicstaticvoidmain(String[]args){

FileOutputStreamfos=null;
ZipInputStreamzis=null;
InputStreamis=null;

try{
ZipFilezf=newZipFile("e:/my.zip");
zis=newZipInputStream(newFileInputStream("e:/my.zip"));
fos=newFileOutputStream("e:/unzip.txt");

//从压缩文件中获取一个条目
ZipEntryentry=zis.getNextEntry();
//获得该条目对象的数据流
is=zf.getInputStream(entry);
byte[]buff=newbyte[1024];
intlen=0;
while((len=is.read(buff))!=-1){
fos.write(buff,0,len);
}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
is.close();
zis.close();
fos.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

}
}
热点内容
怎么开启服务器的ftp 发布:2025-06-13 08:05:25 浏览:643
js无需编译 发布:2025-06-13 07:36:35 浏览:804
linux共享上网 发布:2025-06-13 07:32:53 浏览:532
查询域主机服务器ip 发布:2025-06-13 07:20:13 浏览:121
通过ip进服务器文件看不到了 发布:2025-06-13 07:07:46 浏览:583
linux查看用户的命令 发布:2025-06-13 07:05:37 浏览:173
ftp传输使用的端口是 发布:2025-06-13 06:34:32 浏览:643
linux系统有哪些系统 发布:2025-06-13 06:31:02 浏览:946
hht算法 发布:2025-06-13 06:29:14 浏览:493
电脑开机密码设置怎么设置 发布:2025-06-13 06:28:36 浏览:163