81,092
社区成员
发帖
与我相关
我的任务
分享
public void releaseZipToFile(String sourceZip, String outFileName)
throws IOException {
ZipFile zfile = new ZipFile(sourceZip);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
continue;
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(outFileName, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
is.close();
os.close();
System.out.println("Extracted: " + ze.getName());
}
zfile.close();
File file = new File(sourceZip);
file.delete();
}
// ZipEntry e = createZipEntry(getUTF8String(b, 0, len));
ZipEntry e=null;
try
{
if (this.encoding.toUpperCase().equals("UTF-8"))
e=createZipEntry(getUTF8String(b, 0, len));
else
e=createZipEntry(new String(b,0,len,this.encoding));
}catch(Exception byteE) {
e=createZipEntry(getUTF8String(b, 0, len));
}
public ZipInputStream(InputStream in,String encoding) {
super(new PushbackInputStream(in,512),new Inflater(true),512);
usesDefaultInflater = true;
if(in == null) {
throw new NullPointerException("in is null");
}
this.encoding=encoding;
}
public void unzip(String zipFileName,String outputDirectory) throws Exception{
try {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(zipFileName);
java.util.Enumeration e = zipFile.getEntries();
org.apache.tools.zip.ZipEntry zipEntry = null;
while (e.hasMoreElements()){
zipEntry = (org.apache.tools.zip.ZipEntry)e.nextElement();
System.out.println("unziping "+zipEntry.getName());
if (zipEntry.isDirectory()){
String name=zipEntry.getName();
name=name.substring(0,name.length()-1);
System.out.println("输出路径:"+outputDirectory+File.separator+name);
File f1=new File(outputDirectory+File.separator);
f1.mkdir();
File f=new File(outputDirectory+File.separator+name);
f.mkdir();
System.out.println("创建目录:"+outputDirectory+File.separator+name);
}else{
File f=new File(outputDirectory+File.separator+zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out=new FileOutputStream(f);
//--------解决了图片失真的情况
int c;
byte[] by=new byte[1024];
while((c=in.read(by)) != -1){
out.write(by,0,c);
}
out.close();
in.close();
}
}
}
catch (Exception ex){
}
}