返回列表 发帖

[讨论]关于ASP中在线解压缩rar文件的讨论~!望积极参与~!

发一段JSP用ZIP实现在线压缩
看看能否抛砖引玉
  1. //zip zhe folder
  2. void zip(String zipPath, String srcPath,javax.servlet.jsp.JspWriter out) throws Exception {
  3.     FileOutputStream output = null;
  4.     ZipOutputStream zipOutput = null;
  5.     try{
  6.         output = new FileOutputStream(zipPath);
  7.         zipOutput = new ZipOutputStream(output);
  8.         zipEntry(zipOutput,srcPath,srcPath,zipPath);
  9.     }catch(Exception e){
  10.         out.print("file zip error");
  11.     }finally{
  12.         if(zipOutput!=null)zipOutput.close();
  13.     }
  14.     out.print("zip ok"+zipPath);
  15. }
  16. //add the zip entry
  17. void zipEntry(ZipOutputStream zipOs, String initPath,String filePath,String zipPath) throws Exception {
  18.     String entryName = filePath;
  19.     File f = new File(filePath);
  20.     if (f.isDirectory()){// ??
  21.         String[] files = f.list();
  22.         for(int i = 0; i < files.length; i++)
  23.             zipEntry(zipOs, initPath, filePath + File.separator + files[i],zipPath);
  24.         return;
  25.     }
  26.     String chPh = initPath.substring(initPath.lastIndexOf("/") + 1);// ?????
  27.     int idx=initPath.lastIndexOf(chPh);
  28.     if (idx != -1) {
  29.         entryName = filePath.substring(idx);
  30.     }
  31.     ZipEntry entry;
  32.     entry = new ZipEntry(entryName);
  33.     File ff = new File(filePath);
  34.     if(ff.getAbsolutePath().equals(zipPath))return;
  35.     entry.setSize(ff.length());
  36.     entry.setTime(ff.lastModified());
  37.     //the CRC efficacy  
  38.     entry.setCrc(0);
  39.     CRC32 crc = new CRC32();
  40.     crc.reset();
  41.     zipOs.putNextEntry(entry);
  42.     int len = 0;
  43.     byte[] buffer = new byte[2048];
  44.     int bufferLen = 2048;
  45.     FileInputStream input =null;
  46.     try{
  47.         input = new FileInputStream(filePath);
  48.         while ((len = input.read(buffer, 0, bufferLen)) != -1) {
  49.                 zipOs.write(buffer, 0, len);
  50.                 crc.update(buffer, 0, len);
  51.         }
  52.     }catch(Exception e){
  53.     }finally{
  54.         if(input!=null)input.close();
  55.     }
  56.     entry.setCrc(crc.getValue());
  57. }
复制代码

TOP

返回列表 回复 发帖