Java实现压缩文件夹

/**
 * @param sourceFilePath 待压缩文件(夹)路径
 * @param targetPath 压缩文件所在目录
 * @param zipFileName 压缩后的文件名称{.zip结尾}
 * @return
 * @Description: 创建zip文件
 */
public static boolean createZipFile(String sourceFilePath, String targetPath, String zipFileName){

	boolean flag = false;
	FileOutputStream fos = null;
	ZipOutputStream zos = null;

	// 要压缩的文件资源
	File sourceFile = new File(sourceFilePath);
	// zip文件存放路径
	String zipPath = "";

	if(null != targetPath && !"".equals(targetPath)){
		zipPath = targetPath + File.separator + zipFileName;
	} else {
		zipPath = new File(sourceFilePath).getParent() + File.separator + zipFileName;
	}

	if (sourceFile.exists() == false) {
		System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
		return flag;
	}
	
	try {
		File zipFile = new File(zipPath);
		if (zipFile.exists()) {
			log.error(zipPath + "目录下存在名字为:" + zipFileName + ".zip" + "打包文件.");
		} else {
			File[] sourceFiles = sourceFile.listFiles();
			if (null == sourceFiles || sourceFiles.length < 1) {
				log.error("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
			} else {
				fos = new FileOutputStream(zipPath);
				zos = new ZipOutputStream(new BufferedOutputStream(fos));
				// 生成压缩文件
				writeZip(sourceFile, "", zos);
				flag = true;
			}
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} finally {
		//关闭流
		try {
			if (null != zos) {
				zos.close();
			}
			if (null != fos){
				fos.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	return flag;
}

复制代码
	/**
	 * @param file
	 * @param parentPath
	 * @param zos
	 * @Description:
	 */
private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
	if (file.exists()) {
		// 处理文件夹
		if (file.isDirectory()) {
			parentPath += file.getName() + File.separator;
			File[] files = file.listFiles();
			if (files.length != 0) {
				for (File f : files) {
					// 递归调用
					writeZip(f, parentPath, zos);
				}
			} else {
				// 空目录则创建当前目录的ZipEntry
				try {
					zos.putNextEntry(new ZipEntry(parentPath));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			FileInputStream fis = null;
			try {
				fis = new FileInputStream(file);
				ZipEntry ze = new ZipEntry(parentPath + file.getName());
				zos.putNextEntry(ze);
				byte[] content = new byte[1024];
				int len;
				while ((len = fis.read(content)) != -1) {
					zos.write(content, 0, len);
					zos.flush();
				}
			} catch (FileNotFoundException e) {
				log.error("创建ZIP文件失败", e);
			} catch (IOException e) {
				log.error("创建ZIP文件失败", e);
			} finally {
				try {
					if (fis != null) {
						fis.close();
					}
				} catch (IOException e) {
					log.error("创建ZIP文件失败", e);
				}
			}
		}
	}
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值