package com.sea.elsardcservice.untils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Author: sea
 * @Description:
 * @Date:Created in 下午8:31 19-5-18
 * @Modified By:
 */
public class ZipUtils {

    public static void compressFileToZip(InputStream inputStream, ZipOutputStream zipOutputStream, String name) {

        try {
            ZipEntry zipEntry = new ZipEntry(name);
            zipOutputStream.putNextEntry(zipEntry);
            int len;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                zipOutputStream.write(bytes, 0, len);
                zipOutputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zipOutputStream != null) {
                    zipOutputStream.closeEntry();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted on 2019-05-20 15:07  lshan  阅读(142)  评论(0编辑  收藏  举报