首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

Exception抛出

2014-01-26 
ZipInputStream zipInputStream new ZipInputStream(new FileInputStream(FileName))ZipEntry entry n

  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抛出

------解决方法--------------------------------------------------------
你的写法很乱,我给你发一个压缩代码,一个解压缩你参考一下:
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();

供参考

        

热点排行