java代碼實現壓縮檔

來源:互聯網
上載者:User

   //上傳檔案到伺服器
   String tempPath = req.getSession().getServletContext().getRealPath("webResources/templet/");

   String filename = importFile.getOriginalFilename();  // @RequestParam("importFile") CommonsMultipartFile importFile  作為參數傳遞後台

   //解壓Zip檔案
  String zipPath = tempPath + File.separator + filename;
  String zipFolder = filename.substring(0, filename.length() - 4);
  String decompressPath = tempPath + File.separator + zipFolder;

  List<String> filePathList =readByApacheZipFile(zipPath, decompressPath);

@SuppressWarnings("unchecked")     public List<String> readByApacheZipFile(String zipPath, String decompressPath) throws IOException,
            FileNotFoundException, ZipException {
        File f = new File(decompressPath);
        if (!f.exists()) {
            f.mkdirs();
        }
        List<String> filePathList = new ArrayList<String>();
        ZipFile zf = new ZipFile(zipPath, "GBK");//支援中文  
        Enumeration<ZipEntry> e = zf.getEntries();
        while (e.hasMoreElements()) {
            ZipEntry ze2 = (ZipEntry) e.nextElement();
            String entryName = ze2.getName();
            String path = decompressPath + "/" + entryName;
            if (ze2.isDirectory()) {
                System.out.println("正在建立解壓目錄 - " + entryName);
                File decompressDirFile = new File(path);
                if (!decompressDirFile.exists()) {
                    decompressDirFile.mkdirs();
                }
            } else {
                System.out.println("正在建立解壓檔案 - " + entryName);
                String fileDir = path.substring(0, path.lastIndexOf("/"));
                File fileDirFile = new File(fileDir);
                if (!fileDirFile.exists()) {
                    fileDirFile.mkdirs();
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
                BufferedInputStream bi = new BufferedInputStream(zf.getInputStream(ze2));
                byte[] readContent = new byte[1024];
                int readCount = bi.read(readContent);
                while (readCount != -1) {
                    bos.write(readContent, 0, readCount);
                    readCount = bi.read(readContent);
                }
                bos.close();
                String curDateStr = PortalUtils.getCurrentDateTimeString();
                String renamePath = decompressPath + "/" + entryName.substring(0, entryName.lastIndexOf("/") + 1)
                        + curDateStr + "_" + entryName.substring(entryName.lastIndexOf("/") + 1, entryName.length());
                File ff = new File(path);
                ff.renameTo(new File(renamePath));
                filePathList.add(renamePath);
            }
        }
        zf.close();
        return filePathList;
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.