分享一个 ZIP处理工具类

自己常用的一个zip工具类,能解决项目开发中80%zip处理问题。

pucli class ZipUtil{

	//静态方法:三个参数:文件的二进制,文件路径,文件名
	//通过该方法将在指定目录下添加指定文件
	public static void fileupload(byte[] file, String filePath, String fileName) throws IOException {
		//目标目录
		File targetfile = new File(filePath);
		if (targetfile.exists()) {
			targetfile.mkdirs();
		}

		//二进制流写入
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		out.write(file);
		out.flush();
		out.close();
	}

	private static final int BUFFER_SIZE = 2 * 1024;
	/**-----------------------------------------------zip文件处理方法----------------------------------------------------------*/

	/**
	 * 压缩成ZIP 方法1
	 *
	 * @param srcDir           压缩文件夹路径
	 * @param out              压缩文件输出流
	 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
	 * @throws RuntimeException 压缩失败会抛出运行时异常
	 */
	public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
			throws RuntimeException {

		long start = System.currentTimeMillis();

		ZipOutputStream zos = null;

		try {

			zos = new ZipOutputStream(out);

			File sourceFile = new File(srcDir);

			compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);

			long end = System.currentTimeMillis();

			System.out.println("压缩完成,耗时:" + (end - start) + " ms");

		} catch (Exception e) {

			throw new RuntimeException("zip error from ZipUtils", e);

		} finally {

			if (zos != null) {

				try {

					zos.close();

				} catch (IOException e) {

					e.printStackTrace();

				}
			}
		}
	}


	/**
	 * 压缩成ZIP 方法2
	 *
	 * @param srcFiles 需要压缩的文件列表
	 * @param out      压缩文件输出流
	 * @throws RuntimeException 压缩失败会抛出运行时异常
	 */
	public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {


		ZipOutputStream zos = null;

		try {

			zos = new ZipOutputStream(out);
			for (File srcFile : srcFiles) {

				byte[] buf = new byte[BUFFER_SIZE];
				zos.putNextEntry(new ZipEntry(srcFile.getName()));
				int len;
				FileInputStream in = new FileInputStream(srcFile);
				while ((len = in.read(buf)) != -1) {

					zos.write(buf, 0, len);
				}

				zos.closeEntry();
				in.close();
			}
		} catch (Exception e) {
			throw new RuntimeException("zip error from ZipUtils", e);
		} finally {
			if (zos != null) {
				try {
					zos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 递归压缩方法 方法3
	 *
	 * @param sourceFile       源文件
	 * @param zos              zip输出流
	 * @param name             压缩后的名称
	 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
	 *                         false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
	 * @throws Exception
	 */
	private static void compress(File sourceFile, ZipOutputStream zos, String name,
	                             boolean KeepDirStructure) throws Exception {

		byte[] buf = new byte[BUFFER_SIZE];

		if (sourceFile.isFile()) {
			// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
			zos.putNextEntry(new ZipEntry(name));
			// copy文件到zip输出流中

			int len;
			FileInputStream in = new FileInputStream(sourceFile);
			while ((len = in.read(buf)) != -1) {

				zos.write(buf, 0, len);
			}
			// Complete the entry
			zos.closeEntry();
			in.close();
		} else {
			File[] listFiles = sourceFile.listFiles();
			if (listFiles == null || listFiles.length == 0) {
				// 需要保留原来的文件结构时,需要对空文件夹进行处理
				if (KeepDirStructure) {
					// 空文件夹的处理
					zos.putNextEntry(new ZipEntry(name + "/"));
					// 没有文件,不需要文件的copy
					zos.closeEntry();
				}
			} else {
				for (File file : listFiles) {
					// 判断是否需要保留原来的文件结构
					if (KeepDirStructure) {
						// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
						// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
						compress(file, zos, name + "/" + file.getName(), KeepDirStructure);

					} else {
						compress(file, zos, file.getName(), KeepDirStructure);
					}
				}
			}
		}
	}

	/**
	 * zip文件压缩(单个文件夹、文件压缩)
	 *
	 * @param inputFile  待压缩文件夹/文件名
	 * @param outputFile 生成的压缩包名字
	 */

	public static void ZipCompress(String inputFile, String outputFile) throws Exception {
		//创建zip输出流
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
		//创建缓冲输出流
		BufferedOutputStream bos = new BufferedOutputStream(out);
		File input = new File(inputFile);
		compress(out, bos, input, null);
		bos.close();
		out.close();
	}

	/**
	 * zip文件压缩(整个目录下文件压缩)
	 *
	 * @param inputFile        待压缩文件夹/文件名
	 * @param outputFile       生成的压缩包名字
	 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构,压缩整个文件夹;flase:压缩文件夹下所有文件
	 * @throws RuntimeException 压缩失败会抛出运行时异常
	 */
	public static void ZipCompress(String inputFile, String outputFile, boolean KeepDirStructure) throws Exception {
		//创建zip输出流
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
		File input = new File(inputFile);
		compress(input, out, input.getName(), KeepDirStructure);
		out.close();
	}

	/**
	 * @param name 压缩文件名,可以写为null保持默认
	 */
	//递归压缩
	public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {
		if (name == null) {
			name = input.getName();
		}
		//如果路径为目录(文件夹)
		if (input.isDirectory()) {
			//取出文件夹中的文件(或子文件夹)
			File[] flist = input.listFiles();

			if (flist.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入
			{
				out.putNextEntry(new ZipEntry(name + "/"));
			} else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
			{
				for (int i = 0; i < flist.length; i++) {
					compress(out, bos, flist[i], name + "/" + flist[i].getName());
				}
			}
		} else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
		{
			out.putNextEntry(new ZipEntry(name));
			FileInputStream fos = new FileInputStream(input);
			BufferedInputStream bis = new BufferedInputStream(fos);
			int len = -1;
			//将源文件写入到zip文件中
			byte[] buf = new byte[1024];
			while ((len = bis.read(buf)) != -1) {
				bos.write(buf, 0, len);
			}
			bis.close();
			fos.close();
		}
	}

	/**
	 * zip解压
	 *
	 * @param inputFile   待解压文件名
	 * @param destDirPath 解压路径
	 */

	@SuppressWarnings("resource")
	public static void ZipUncompress(String inputFile,String destDirPath) throws Exception {
		File srcFile = new File(inputFile);//获取当前压缩文件
		// 判断源文件是否存在
		if (!srcFile.exists()) {
			throw new Exception(srcFile.getPath() + "所指文件不存在");
		}
		//开始解压
		//构建解压输入流
		ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile));
		ZipEntry entry = null;
		File file = null;
		while ((entry = zIn.getNextEntry()) != null) {
			if (!entry.isDirectory()) {
				file = new File(destDirPath, entry.getName());
				if (!file.exists()) {
					new File(file.getParent()).mkdirs();//创建此文件的上级目录
				}
				OutputStream out = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(out);
				int len = -1;
				byte[] buf = new byte[1024];
				while ((len = zIn.read(buf)) != -1) {
					bos.write(buf, 0, len);
				}
				// 关流顺序,先打开的后关闭
				bos.close();
				out.close();
			}
		}
	}

	/**
	 * zip解压
	 * @param srcFile 待解压文件
	 * @param destDirPath  解压路径
	 */

	public static void ZipUncompress(MultipartFile srcFile, String destDirPath) throws Exception {

		//开始解压
		//构建解压输入流
		ZipInputStream zIn = new ZipInputStream(srcFile.getInputStream());
		ZipEntry entry = null;
		File file = null;
		while ((entry = zIn.getNextEntry()) != null) {
			if (!entry.isDirectory()) {
				file = new File(destDirPath, entry.getName());
				if (!file.exists()) {
					new File(file.getParent()).mkdirs();//创建此文件的上级目录
				}
				OutputStream out = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(out);
				int len = -1;
				byte[] buf = new byte[1024];
				while ((len = zIn.read(buf)) != -1) {
					bos.write(buf, 0, len);
				}
				// 关流顺序,先打开的后关闭
				bos.close();
				out.close();
			}
		}
	}


	public static void main(String[] args) {
		try {
			ZipCompress("C:\\Users\\wujiafu\\Desktop\\csv", "C:\\Users\\wujiafu\\Desktop\\csv\\TestbyYTT.zip",true);
			ZipUncompress("C:\\Users\\wujiafu\\Desktop\\csv\\TestbyYTT.zip","C:\\Users\\wujiafu\\Desktop\\2");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值