java 实现压缩和解压

先检测一下maven 因为要使用到sum包这个包是在jdk里 ,检测一下是否一直,不让可能会在打包中出现找不到sum包

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import lombok.SneakyThrows;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author 29027
 */
public class FileZipUtils {

       public static String path = "/home/data/sw/file/";
    /**
     * 对压缩文件进行解压并转换成输入流
     *
     * @param file 要解压的文件
     * @return Map key文件名称     InputStream 输入流path
     */
    @SneakyThrows
    public static Map<String, InputStream> decompress(MultipartFile file) {
        Map<String, InputStream> map = new HashMap<>();
        //转换成io流
        InputStream inputStream = file.getInputStream();
        ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("UTF-8"));
        List<InputStream> list = new ArrayList<>();
        ZipEntry zipEntry = null;
        //是否存在下一个文件
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            BufferedReader in = null;
            byte[] byte_s = new byte[1];
            int num = -1;
            //进行读取和写入一个文件
            while ((num = zipInputStream.read(byte_s, 0, byte_s.length)) > -1) {
                byteArrayOutputStream.write(byte_s);
            }
            //此处可以重新设置因为因为的需求
            map.put(zipEntry.getName(), new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
            byteArrayOutputStream.close();
        }
        zipInputStream.close();
        inputStream.close();
        return map;
    }

    /**
     * 对压缩文件进行解压并转换成输入流
     *
     * @param file 要解压的文件
     * @return Map key文件名称     InputStream 输入流path
     */
    @SneakyThrows
    public static Map<String, InputStream> decompress(File file) {
        Map<String, InputStream> map = new HashMap<>();
        //转换成io流
        InputStream inputStream = new FileInputStream(file);
        ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName("UTF-8"));
        List<InputStream> list = new ArrayList<>();
        ZipEntry zipEntry = null;
        //是否存在下一个文件
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            BufferedReader in = null;
            byte[] byte_s = new byte[1];
            int num = -1;
            //进行读取和写入一个文件
            while ((num = zipInputStream.read(byte_s, 0, byte_s.length)) > -1) {
                byteArrayOutputStream.write(byte_s);
            }
            //此处可以重新设置因为因为的需求
            map.put(zipEntry.getName(), new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
            byteArrayOutputStream.close();
        }
        zipInputStream.close();
        inputStream.close();
        return map;
    }
    //压缩

    /**
     * 吧指定文件的字节流进行压缩并下载给客户端 可以引用在导出 
     *
     * @param list      要压缩的字节流
     * @param response  客户端的相应
     * @param fileNames 文件名称
     * @return ZipOutputStream 压缩输出流
     */
    @SneakyThrows
    public static ZipOutputStream compression(List<ByteOutputStream> list, HttpServletResponse response, List<String> fileNames) {
        ZipOutputStream zipOutputStream = null;
        zipOutputStream = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
        // 设置压缩方法
        zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
        for (int i = 0; i < list.size(); i++) {
            list.get(i);
            //文件名称因为因为需求可以自定义配置
            String fileName = fileNames.get(i);
            zipOutputStream.putNextEntry(new ZipEntry(fileName));
            InputStream is = new ByteArrayInputStream(list.get(i).getBytes());
            byte[] b = new byte[1];
            int length = 0;
            while ((length = is.read(b)) > -1) {
                zipOutputStream.write(b, 0, length);
            }
            zipOutputStream.closeEntry();
        }
        zipOutputStream.close();
        return zipOutputStream;
    }


    /**
     * 吧指定文件的字节流进行压缩并下载给客户端
     *
     * @return ZipOutputStream 压缩输出流
     */
    @SneakyThrows
    public static ZipOutputStream compression(List<File> fileList,HttpServletResponse response) {
        ZipOutputStream zipOutputStream = null;
        zipOutputStream = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
        // 设置压缩方法
        zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
        for (int i = 0; i < fileList.size(); i++) {
            fileList.get(i);
            //文件名称因为因为需求可以自定义配置
            String fileName = fileList.get(i).getName();
            zipOutputStream.putNextEntry(new ZipEntry(fileName));
            InputStream is = new FileInputStream(fileList.get(i));
            byte[] b = new byte[1];
            int length = 0;
            while ((length = is.read(b)) > -1) {
                zipOutputStream.write(b, 0, length);
            }
            zipOutputStream.closeEntry();
        }
        zipOutputStream.close();
        return zipOutputStream;
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值