java.util.zip获取Zip文件条目InputStream

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.util.zip获取Zip文件条目InputStream相关的知识,希望对你有一定的参考价值。

package com.test;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.tool.zip.InflaterInputStream;
import com.tool.zip.ZipEntry;
import com.tool.zip.ZipInputStream;

public class TestZip {

    //private static String tempZip = "f:\\tempZip.zip";
    /**
     * 
     * @param input Zip文件的流
     * @throws Exception
     */
    public void upload(InputStream input) throws Exception {

        // System.setProperty("sun.zip.encoding",
        // System.getProperty("sun.jnu.encoding"));

        ZipInputStream zis = new ZipInputStream(input);
        ZipEntry entry = null;
        
        while ((entry = zis.getNextEntry()) != null) {

            // System.out.printf("条目信息: 名称%1$b, 大小%2$d, 压缩时间%3$d \n",
            // entry.getName(), entry.getSize(), entry.getTime());

            if (entry.isDirectory()) { // is dir 
                // System.out.println(entry.getName() + "是一个目录");
                File f = new File("f:" + File.separator + entry.getName());
                if (!f.exists())
                    f.mkdirs();
            } else { //

                byte[] data = getByte(zis); // 获取当前条目的字节数组

                InputStream is = new ByteArrayInputStream(data); // 把当前条目的字节数据转换成Inputstream流
                String[] names = entry.getName().split("/");
                
                String path = "f:" + File.separator;

                path += join(names, File.separator);

                //System.out.println(path);
                File file = new File(path);
                if (!file.exists()) {
                    file.createNewFile();
                    toWrite(is, file);
                }

            }
        }
    }
    /**
     * 向file文件写入字节
     * @param ins
     * @param file
     */
    public static void toWrite(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 获取条目byte[]字节
     * @param zis 
     * @return
     */
    public byte[] getByte(InflaterInputStream zis) {
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            byte[] buf = null;
            int length = 0;

            while ((length = zis.read(temp, 0, 1024)) != -1) {
                bout.write(temp, 0, length);
            }

            buf = bout.toByteArray();
            bout.close();
            return buf;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static String join(Object[] o, String flag) {
        StringBuffer str_buff = new StringBuffer();

        for (int i = 0, len = o.length; i < len; i++) {
            str_buff.append(String.valueOf(o[i]));
            if (i < len - 1)
                str_buff.append(flag);
        }

        return str_buff.toString();
    }

    // test method
    public static void main(String[] args) throws Exception {
        TestZip test = new TestZip();
        String filePath = "f:\\test.zip";
        // File file = new File(filePath);
        InputStream input = new BufferedInputStream(new FileInputStream(
                filePath));

        test.upload(input);
    }
}

 

以上是关于java.util.zip获取Zip文件条目InputStream的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Android 中的“TransformException:java.util.zip.ZipException:重复条目”?

为啥我在使用 java.util.zip.ZipFile 打开空 ZIP 文件时出现异常?

如何修复 java.util.zip.ZipException:重复条目:com/google/firebase/FirebaseApiNotAvailableException.class?

Gradle 构建失败:java.util.zip.ZipException:重复条目:org/apache/commons/io/CopyUtils.class

com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:com/google/androi

如何解决java.util.zip.ZipException