Java压缩文件生成工具类

在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

/** 
 * @project: Test 
 * @author chenssy
 * @date 2013-7-28 
 * @Description: 文件压缩工具类
 * 				  将指定文件/文件夹压缩成zip、rar压缩文件
 */
public class CompressedFileUtil {
	/**
	 * 默认构造函数
	 */
	public CompressedFileUtil(){

	}

	/**
	 * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip
	 * @param resourePath 源文件/文件夹
	 * @param targetPath  目的压缩文件保存路径
	 * @return void
	 * @throws Exception 
	 */
	public void compressedFile(String resourcesPath,String targetPath) throws Exception{
		File resourcesFile = new File(resourcesPath);     //源文件
		File targetFile = new File(targetPath);           //目的
		//如果目的路径不存在,则新建
		if(!targetFile.exists()){     
			targetFile.mkdirs();  
		}

		String targetName = resourcesFile.getName()+".zip";   //目的压缩文件名
		FileOutputStream outputStream = new FileOutputStream(targetPath+"//"+targetName);
		ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));

		createCompressedFile(out, resourcesFile, "");

		out.close();  
	}

	/**
	 * @desc 生成压缩文件。
	 * 	             如果是文件夹,则使用递归,进行文件遍历、压缩
	 *       如果是文件,直接压缩
	 * @param out  输出流
	 * @param file  目标文件
	 * @return void
	 * @throws Exception 
	 */
	public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
		//如果当前的是文件夹,则进行进一步处理
		if(file.isDirectory()){
			//得到文件列表信息
			File[] files = file.listFiles();
			//将文件夹添加到下一级打包目录
			out.putNextEntry(new ZipEntry(dir+"/"));

			dir = dir.length() == 0 ? "" : dir +"/";

			//循环将文件夹中的文件打包
			for(int i = 0 ; i < files.length ; i++){
				createCompressedFile(out, files[i], dir + files[i].getName());         //递归处理
			}
		}
		else{   //当前的是文件,打包处理
			//文件输入流
			FileInputStream fis = new FileInputStream(file);

			out.putNextEntry(new ZipEntry(dir));
			//进行写操作
			int j =  0;
			byte[] buffer = new byte[1024];
			while((j = fis.read(buffer)) > 0){
				out.write(buffer,0,j);
			}
			//关闭输入流
			fis.close();
		}
	}

	public static void main(String[] args){
		CompressedFileUtil compressedFileUtil = new CompressedFileUtil();

		try {
			compressedFileUtil.compressedFile("G://zip", "F://zip");
			System.out.println("压缩文件已经生成...");
		} catch (Exception e) {
			System.out.println("压缩文件生成失败...");
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Apache PDFBox库来生成OFD文件,因为OFD文件本质上是一个包含XML和一些其他文件的ZIP文件。以下是一个示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class OFDGenerator { public static void generateOFD() throws IOException, JAXBException { // 创建OFD文件夹 File ofdFolder = new File("example.ofd"); ofdFolder.mkdirs(); // 创建OFD.xml文件 OFD ofd = new OFD(); JAXBContext jaxbContext = JAXBContext.newInstance(OFD.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(ofd, new File(ofdFolder, "OFD.xml")); // 将OFD.xml和其他文件压缩为OFD文件 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("example.ofd")); addToZipFile("OFD.xml", ofdFolder.getAbsolutePath(), zipOut); // 添加其他文件 zipOut.close(); } private static void addToZipFile(String fileName, String folderName, ZipOutputStream zipOut) throws IOException { File file = new File(folderName + "/" + fileName); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } fis.close(); } } ``` 请注意,这只是一个简单的示例,你需要根据你的具体需求来更改代码。此外,你还需要确保所使用的Apache PDFBox库版本支持OFD生成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值