日期:2014-05-20  浏览次数:20622 次

请大家看看我关于压缩ZIP的CODE,现在总是有Exception抛出
                ZipInputStream   zipInputStream   =   new   ZipInputStream(new   FileInputStream(FileName));
                ZipEntry   entry   =   null;
                while((entry   =   zipInputStream.getNextEntry())   !=   null){
                        inputStream   =   zipInputStream;
                        outputStream.putNextEntry(entry);
                        while((readLength   =   inputStream.read(inputBuffer))   >   0)
{
        outputStream.write(inputBuffer,   0,   readLength);
}
                        outputStream.closeEntry();//Exception
                }

最后一行的时候会有个Exception抛出

------解决方案--------------------
lz的写法很乱,我给你发一个压缩代码,一个解压缩你参考一下:
1,unZip:
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
while (true) {
ZipEntry nextEntry = zipInputStream.getNextEntry();
if (nextEntry == null) {
break;
}
if (nextEntry.isDirectory()) {
throw new DSWFException( "上传压缩包中包含文件夹! ");
}
String name = nextEntry.getName();
......
}

2,zip:
File newZip = new File( "压缩文件名.zip ");
FileOutputStream out = new FileOutputStream(newZip);
ZipOutputStream zipOutputStream = new ZipOutputStream(out);
File []inputFiles = rootFile.listFiles();//要压缩的文件列表
for (int k = 0; k < inputFiles.length;k ++) {
ZipEntry zipEntry = new ZipEntry(inputFiles[k].getName());
zipOutputStream.putNextEntry(zipEntry);
FileInputStream fis = new FileInputStream(inputFiles[k]);
while (true) {
int b = fis.read();
if (b == -1) {
break;
}
zipOutputStream.write(b);
}
fis.close();
zipOutputStream.closeEntry();
}
zipOutputStream.close();

共参考