package com
.chuangqi
.tools
;
import org
.apache
.tools
.zip
.ZipEntry
;
import org
.apache
.tools
.zip
.ZipFile
;
import java
.io
.*;
import java
.util
.Enumeration
;
public class FileUnZipUtils
{
public
static void zipToFile(String sourceFile
, String toFolder
) throws Exception
{
String toDisk
= toFolder
;
ZipFile zfile
= new
ZipFile(sourceFile
, "gbk");
Enumeration zList
= zfile
.getEntries();
ZipEntry ze
= null
;
byte
[] buf
= new byte
[1024];
while (zList
.hasMoreElements()) {
ze
= (ZipEntry
) zList
.nextElement();
if (ze
.isDirectory()) {
File f
= new
File(toDisk
+"/" + ze
.getName());
f
.mkdirs();
continue;
}
OutputStream outputStream
= null
;
InputStream inputStream
= null
;
try
{
outputStream
= new
BufferedOutputStream(new
FileOutputStream(getRealFileName(toDisk
, ze
.getName())));
inputStream
= new
BufferedInputStream(zfile
.getInputStream(ze
));
int readLen
= 0;
while ((readLen
= inputStream
.read(buf
, 0, 1024)) != -1) {
outputStream
.write(buf
, 0, readLen
);
}
inputStream
.close();
outputStream
.close();
} catch
(Exception e
) {
throw new
IOException("解压失败:" + e
.toString());
} finally
{
if (inputStream
!= null
) {
try
{
inputStream
.close();
} catch
(IOException ex
) {
}
}
if (outputStream
!= null
) {
try
{
outputStream
.close();
} catch
(IOException ex
) {
ex
.printStackTrace();
}
}
inputStream
= null
;
outputStream
= null
;
}
}
zfile
.close();
}
private
static File
getRealFileName(String zippath
, String absFileName
) {
String
[] dirs
= absFileName
.split("/", absFileName
.length());
File ret
= new
File(zippath
);
if (dirs
.length
> 1) {
for (int i
= 0; i
< dirs
.length
- 1; i
++) {
ret
= new
File(ret
, dirs
[i
]);
}
}
if (!ret
.exists()) {
ret
.mkdirs();
}
ret
= new
File(ret
, dirs
[dirs
.length
- 1]);
return ret
;
}
}
转载请注明原文地址:https://tech.qufami.com/read-7043.html