javaunzip
Ⅰ 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();
}
}
}
}