java不解压zip文件时,获取其中的文件

来源:互联网 发布:卧龙大数据技术总监 编辑:程序博客网 时间:2024/04/30 15:14

直接上代码

public static void unzip(InputStream ins, String targetFile) {
  BufferedOutputStream dest = null;
  ZipEntry entry;
  ZipInputStream zin = null;
  FileOutputStream fos = null;
  try {
   zin = new ZipInputStream(new BufferedInputStream(ins));
   while ((entry = zin.getNextEntry()) != null) {

    //判断是否是目录
    if(!entry.isDirectory()){
     System.out.println("Extracting: " + entry);
     int count;
     byte data[] = new byte[BUFFER];
     String filePath = targetFile + "/" + entry.getName();
     String path = filePath.substring(0, filePath.lastIndexOf("/"));
     File dir = new File(path);
     if (!dir.exists()) {
      dir.mkdirs();
     }
     System.out.println("图片名称=entry===" + entry);
     System.out.println("图片名称=entry.getName()===" + entry.getName());
     System.out.println("图片名称=entry.getName().toLowerCase()===" + entry.getName().toLowerCase());
     // fos = new FileOutputStream(targetFile + "/"
     // + entry.getName().toLowerCase());
     String picName = entry.getName().substring(entry.getName().indexOf("/")+1);
     System.out.println("图片名称=picName===" + picName);
     fos = new FileOutputStream(targetFile + "/" + picName);
     dest = new BufferedOutputStream(fos, BUFFER);
     while ((count = zin.read(data, 0, BUFFER)) != -1) {
      dest.write(data, 0, count);
     }
     dest.flush();
     dest.close();
    }
   }
   zin.close();

   fos.flush();
   fos.close();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (dest != null) {
     dest.close();
    }
    if (zin != null) {
     zin.close();
    }
    if (fos != null) {
     fos.close();
    }

   } catch (Exception e) {

   }

  }
 }


0 0
原创粉丝点击