将InputStream—zip文件流直接解压缩

/**
 * Project Name:DemoMer
 * File Name:zipUtil.java
 * Date:2018/10/15 11:10
 * Copyright (c) 2018.
 */


/**
 * ClassName: zipUtil 
 * Function: TODO ADD FUNCTION. 
 * Date: 2018/10/15 11:10 
 * @author li
 * @version
 * @since JDK 1.8
 */

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipInputStream;

//import org.apache.tools.zip.ZipFile;

/**
 * 解压Zip文件工具类
 * @author zhangyongbo
 *
 */
public class ZipUtil {

    /**
     *
     * @param input Zip文件的流
     * @throws Exception
     */
    public void upload(InputStream input,String destPath) throws Exception {

        ZipInputStream zis = new ZipInputStream(input);
        java.util.zip.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(destPath + 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 = destPath + 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();
    }

    public static void main(String[] args) throws Exception {
        TestZip test = new TestZip();
        String filePath = "D:\\20887212384979710156_20180701_DETAILS.zip";
        // File file = new File(filePath);
        InputStream input = new BufferedInputStream(new FileInputStream(filePath));
        test.upload(input,"E:\\work");
    }
}

转载:https://www.cnblogs.com/chiangfai/p/5888141.html

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用Java的ZipFile类来解压缩zip文件。如果嵌套的zip文件也需要被解压缩,可以使用递归方法来遍历所有的zip文件解压缩。 以下是一个简单的Java代码示例,可以解压缩所有嵌套的zip文件: ```java import java.io.*; import java.util.zip.*; public class UnzipAll { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String destDirPath = "path/to/your/destination/directory"; unzipAll(zipFilePath, destDirPath); } public static void unzipAll(String zipFilePath, String destDirPath) { try { File destDir = new File(destDirPath); if (!destDir.exists()) { destDir.mkdir(); } ZipFile zipFile = new ZipFile(zipFilePath); ZipEntry entry; String entryName; File entryFile, entryDir; InputStream inStream; OutputStream outStream; byte[] buffer = new byte[1024]; int bytesRead; for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) { entry = (ZipEntry) e.nextElement(); entryName = entry.getName(); entryFile = new File(destDir, entryName); if (entry.isDirectory()) { entryFile.mkdirs(); } else { entryDir = new File(entryFile.getParent()); if (!entryDir.exists()) { entryDir.mkdirs(); } inStream = zipFile.getInputStream(entry); outStream = new FileOutputStream(entryFile); while ((bytesRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } outStream.close(); inStream.close(); if (entryName.endsWith(".zip")) { unzipAll(entryFile.getAbsolutePath(), entryDir.getAbsolutePath()); } } } zipFile.close(); } catch (IOException ex) { ex.printStackTrace(); } } } ``` 在这个示例中,`unzipAll`方法接收一个zip文件的路径和一个目标目录的路径,并递归地解压缩所有嵌套的zip文件。如果一个zip文件解压缩,它将被保存到与其同级的目录中。 你可以将 `zipFilePath` 和 `destDirPath` 替换为你自己的实际值,然后运行这个示例来解压缩所有嵌套的zip文件
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值