Java ZIP文件解壓

Java ZIP文件解壓 備忘筆記

代碼:

    private byte[] unZip(byte[] data) {
        byte[] bArr = null;
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
            ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);
            ZipEntry zipEntry = null;

            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                if (zipEntry.isDirectory()) continue;

                byte[] buffer = new byte[1024];
                int num = -1;
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                while ((num = zipInputStream.read(buffer, 0, buffer.length)) != -1) {
                    outputStream.write(buffer, 0, num);
                }

                bArr = outputStream.toByteArray();
                outputStream.flush();
                outputStream.close();

                if (zipEntry.getName().toUpperCase().equals("GAB_ZIP_INDEX.XML")) { //索引文件
                    String xmlStr = new String(bArr, "utf-8");
                    indexXml = getIndexXml(xmlStr);
                }

                if (zipEntry.getName().toUpperCase().indexOf(".BCP") > 0) { //bcp數據文件
                    String bcpStr = new String(bArr, "utf-8");
                    bcpContent.put(zipEntry.getName(), bcpStr);
                }
            }

            zipInputStream.close();
            byteArrayInputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return bArr;
    }
View Code

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章