首页 > 编程语言 >用Java压缩文件(夹)

用Java压缩文件(夹)

时间:2023-01-24 17:33:49浏览次数:39  
标签:Java String doCompress 压缩文件 File inFile dir out

Java压缩文件/文件夹

应用场景:
备份文件,然而,某网盘不让上传大文件,

那就一个文件夹一个文件夹地压缩,再上传。

手动压缩太麻烦,故用代码压缩之。

package com.ah.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 参考:https://www.cnblogs.com/daofu/p/16195729.html
 */
public class AhZipUtils {
	public static void doCompress(String srcFilePath) {
		String date = AhTimeUtils.getYMD();
		doCompress(srcFilePath, srcFilePath + "_" + date + ".zip");
	}

	public static void doCompress(String srcFilePath, String zipFilePath) {
		File srcFile = new File(srcFilePath);
		if (!srcFile.exists()) {
			AhCommonUtils.printlog("需要压缩的文件(夹)不存在:" + srcFilePath);
			return;
		}
		File zipFile = new File(zipFilePath);
		AhFileUtils.deletePath(zipFilePath);
		long showTime1_start = AhCommonUtils.showTime1_start();
		System.out.println("开始压缩:" + srcFilePath);
		doCompress(srcFile, zipFile);
		AhCommonUtils.showTime2_end(showTime1_start);
		System.out.println("压缩完成:" + zipFilePath);
	}

	/**
	 * 文件压缩
	 * 
	 * @param srcFile 目录或者单个文件
	 * @param zipFile 压缩后的ZIP文件
	 */
	private static void doCompress(File srcFile, File zipFile) {

		try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
			String dir = "";
			doCompress(srcFile, out, dir);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param inFile
	 * @param out
	 * @param dir:解压出来的的文件,会放在这个文件夹中
	 * @throws IOException
	 */
	private static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
		if (inFile.isDirectory()) {
			File[] files = inFile.listFiles();
			if (files != null && files.length > 0) {
				for (File file : files) {
					String name = inFile.getName();
					if (!"".equals(dir)) {
						name = dir + "/" + name;
					}
					doCompress(file, out, name);
				}
			}
		} else {
			doZip(inFile, out, dir);
		}
	}

	private static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
		String entryName = null;
		if (!"".equals(dir)) {
			entryName = dir + "/" + inFile.getName();
		} else {
			entryName = inFile.getName();
		}
		ZipEntry entry = new ZipEntry(entryName);
		out.putNextEntry(entry);
		int len = 0;
		byte[] buffer = new byte[1024];
		FileInputStream fis = new FileInputStream(inFile);
		while ((len = fis.read(buffer)) > 0) {
			out.write(buffer, 0, len);
			out.flush();
		}
		out.closeEntry();
		fis.close();
	}

	/**
	 * 正式使用,非测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		AhZipUtils.doCompress("/Users/Documents/AH_Code/AhProjData1");
        AhZipUtils.doCompress("/Users/Documents/AH_Code/AhProjData2");
	}
}

标签:Java,String,doCompress,压缩文件,File,inFile,dir,out
From: https://www.cnblogs.com/tigerlion/p/17066206.html

相关文章

  • JavaScript学习笔记—闭包
    1.定义闭包就是能访问到外部函数作用域中变量的函数2.什么时候使用当需要隐藏一些不希望被别人访问的内容时就可以使用闭包3.构成要件(1)函数的嵌套(2)内部函数要引用......
  • Windows11系统下配置JAVA环境变量(JDK-19版本)
    JDK下载1、访问oracle官网https://www.oracle.com/2、点击导航条中的Resources,点击DeveloperDownlads进入3、继续点击Java进入4、继续点击Java(JDK)for......
  • JavaScript: div,textarea set or get value
    <!doctypehtml><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,u......
  • JAVA的注释和变量名称
    1.注释(1)单行注释为//.....例://这是一行注释(2)多行注释是/*.......................*/例:/*我是第一行注释我是第二行注释我是第三行注释*/(3)文档注释/**................
  • 【学懂Java】(五)异常处理
    一.引入编写程序时会有各种各样的错误,例如该程序在被除数为0的时候,就会不正确。我们可以这样解决但是这样也有弊端:代码臃肿程序员要花很大精力“堵漏洞”程序员很难堵住所有......
  • 安装部署Java项目
    开头:之前做了个文档转换的小项目,想部署在安卓手机上,自己可以随时看看,所以才有了下面这篇文章,内容或有瑕疵,望请批正。文末放我自己部署文档转换网址,仅供大家参考,谢谢!选择:Te......
  • 4.1 JavaScript 原始值与引用值
    JavaScript中有两种类型的值:原始值和引用值。原始值是不可变的,如Undefined、Null、Boolean、Number、String和Symbol。当将一个原始值赋值给另一个变量时,实际上是将该值......
  • Oracle宣布Java7生命周期终结
     Oracle于2015年4月停止在公共渠道发布Java7安全补丁和升级包,以敦促用户迁移至Java8或购买Java7的长期商业支持服务。后续可能有其它第三方机构为其提供公......
  • JavaScript 自增和自减运算符
    JavaScript中有两种方法来自增或自减一个变量的值,分别是自增运算符和自减运算符。自增运算符(++):将变量的值增加1。letx=5;x++;//x现在的值为6自减运算符(--):将......
  • Javascript Basic
    Javascriptisa dynamicallytyped, weaklytypedand interpreted high-levelprogramminglanguage.InterestingJavaScriptFeaturesWe'llgoovertheseinmor......