package com.gci.boot.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import lombok.extern.slf4j.Slf4j;

/**
 * 生成压缩包
 * @author admin
 *
 */
@Slf4j
public class ZipUtil {

	/** * 解压缩ZIP文件 * @param zipFile ZIP文件 * @param destDir 目标路径 */
	public static void unzip(InputStream zipFile, String dest) {
		File destDir = new File(dest);
		byte[] buffer = new byte[1024];
		try (ZipInputStream zis = new ZipInputStream(zipFile,Charset.forName("GBK"))) {
			ZipEntry entry = zis.getNextEntry();
			while (entry != null) {
				File file = new File(destDir, entry.getName());
				if (entry.isDirectory()) {
					file.mkdirs();
				} else {
					File parent = file.getParentFile();
					if (!parent.exists()) {
						parent.mkdirs();
					}
					try (FileOutputStream fos = new FileOutputStream(file)) {
						int len;
						while ((len = zis.read(buffer)) > 0) {
							fos.write(buffer, 0, len);
						}
					}
				}
				entry = zis.getNextEntry();
			}
		} catch (IOException e) {
			log.error("解压文件出错", e);
		}
	}
	
}
