解压zip

来源:互联网 发布:刘义军 知乎 编辑:程序博客网 时间:2024/05/01 13:22
@RequestMapping(method = RequestMethod.POST)
public String doUpload(HttpServletRequest request,
@RequestParam("package_name") MultipartFile mfile)throws Exception {
try {
String dest = Configuration.getInstance().get("upload.basepath");

InputStream input = mfile.getInputStream();
ZipInputStream zipInput = new ZipInputStream(input);

FileOutputStream fileOut; 
       File file;
       int readedBytes = 0;
       byte[] buf = new byte[512];
       ZipEntry zipEntry;
      
       while( (zipEntry = zipInput.getNextEntry()) != null){
       
        file = new File(dest + "/" + zipEntry.getName());
        if(zipEntry.isDirectory()){
        file.mkdirs();
        }else{
        File parent = file.getParentFile();
       
        if(!parent.exists()){
        parent.mkdirs();
        }
       
        fileOut = new FileOutputStream(file);
        while((readedBytes = zipInput.read(buf)) > 0){
        fileOut.write(buf, 0, readedBytes);
        }
        fileOut.close();
        }
        zipInput.closeEntry();
       }
} catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {
            e.printStackTrace();   
        }
原创粉丝点击