java多重封装解压ZIP文件

java多重打包解压ZIP文件
今天用到了ZIP,涉及多重打包解压zip

在网上搜了下,不符合自己的要求,就自己写了个

psa(zip文件)文件下面有两个文件,.info文本文件和.ops的zip文件

代码如下,主要是代买框架,测试无误,主要是多重压缩的时候出问题了,哎,瓜呀,现在好了,可以用了

压缩
/**
 * Relicure Interactive Co. Ltd. Copyright 2012
 *
 * $Id$
 * Last Modified By: $Author$ on $DataTime$
 */

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author qifan.yang
 */
public class ZipArchive {

    private void run() throws IOException {
        //导出的目录
        String path = "D:\\particle";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }

        StringBuilder info = new StringBuilder();
        //info cfg        测试数据
        info.append("PS v1.0\n").append("ps_name").append("\n{");
        info.append("\ntexture ").append("path......");
        info.append("\ntranslate ").append("0.0").append(" ").append("100.793").append(" ").append("1.22959E-8");
        info.append("\nscale ").append("1.0").append(" ").append("0.830375").append(" ").append("1.0");
        info.append("\nrotate ").append("0.0").append(" ").append("1.0").append(" ").append("1.0");
        info.append("\n}\n");

        FileOutputStream dest = new FileOutputStream("D:\\particle\\abc.zip");  //输出文件
        BufferedOutputStream buffOut = new BufferedOutputStream(dest);
        ZipOutputStream out = new ZipOutputStream(buffOut);
        // 压缩info
        zipFile(out, new ByteArrayInputStream(info.toString().getBytes()), "ps_name_cfg.info");
        buffOut.flush();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024 * 1024);
        ZipOutputStream xmlOut = new ZipOutputStream(byteArrayOutputStream);

        zipFile(xmlOut, new ByteArrayInputStream(("this is xml out test !"+info.toString()).getBytes()), "ps.xml");
        xmlOut.flush();
        xmlOut.finish();

        ByteArrayInputStream innerInputSteam = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        zipFile(out, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), "inner.zip");
//        ZipEntry nextEntry = zipInputStream.getNextEntry();  //得到刚才写的ps.xml ZIPEntry
//        ZipEntry opsEntry = new ZipEntry("内部.zip");  //中文要乱码
//        ZipEntry opsEntry = new ZipEntry("inner.zip");
//        out.putNextEntry(opsEntry);
//        int len;
//        while ((len = innerInputSteam.read()) != -1) {
//            out.write(len);
//        }
        out.flush();
        out.close();

    }

    public void zipFile(ZipOutputStream zos, ByteArrayInputStream inputStream, String fileName) {
        try {
            zos.putNextEntry(new ZipEntry(fileName));
            int len;
            while ((len = inputStream.read()) != -1) {
                zos.write(len);
            }
//            zos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        new ZipArchive().run();
    }
}



解压

/**
 * Relicure Interactive Co. Ltd. Copyright 2012
 *
 * $Id$
 * Last Modified By: $Author$ on $DataTime$
 */

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * @author qifan.yang
 */
public class ZipUnArchive {

    private void run() throws IOException {
        ZipFile zipFile = new ZipFile("D:/ps1.psa");//pas是zip文件

        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        ZipEntry entry;
        InputStream inputStream = null;

        while (entries.hasMoreElements()) {
            entry = entries.nextElement();

            System.out.println(entry.toString() + "\n");
            if (entry.toString().endsWith(".info")) {//info是文本文件
                inputStream = zipFile.getInputStream(entry);
                //TODO 这儿读取文件内容
//                loadINFO(inputStream);
            }
            if (entry.toString().endsWith(".ops")) {//ops是zip文件
                //TODO 读取多重压缩文件
                inputStream = zipFile.getInputStream(entry);
                ZipInputStream zipInputStream = new ZipInputStream(inputStream);
                zipInputStream.getNextEntry();//定位到下个文件头部
                //TODO 这儿读取文件内容
//                loadOPS(zipInputStream);

            }

        }

        inputStream.close();
        zipFile.close();

    }

    public static void main(String[] args) throws IOException {
        new ZipUnArchive().run();
    }

}