从字节数组创建ZIP

时间:2013-03-02 06:25:45

标签: java android bytearray unzip zipfile

我从服务器发送的JSON响应中获取一个表示ZIP文件的字节数组,而在Android端,我想从数组中重新创建ZIP。

我正在使用此代码来实现此功能,但它并未在我的设备存储中创建ZIP文件。

从JSON中提取字节数组:

String zipBinary = mJsonObject.getString("zip");

存储zip文件的文件路径:

String unzipLocation = Environment.getExternalStorageDirectory() + "/sample.zip"; 

主要代码:

public CreateZipMultipleFiles(String filePath, byte[] sourceFile) {
    try {
        ByteArrayOutputStream fout = new ByteArrayOutputStream();

        ZipOutputStream zout = new ZipOutputStream(fout);
        ZipEntry ze = new ZipEntry(filePath);
        ze.setSize(sourceFile.length);
        zout.putNextEntry(ze);
        zout.write(sourceFile);
        zout.closeEntry();

        zout.close();

        System.out.println("Zip Byte file has been created!");
    } catch (IOException ioe) {
        System.out.println("IOException :" + ioe);
    }
}

我如何实现这一目标?

0 个答案:

没有答案