Java将指定文件/文件夹压缩成zip、rar压缩文件
2014-06-17 16:27
597 查看
import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import java.io.*; import java.util.zip.CheckedOutputStream; import java.util.zip.CRC32; /** * @author chenssy * * 将指定文件/文件夹压缩成zip、rar压缩文件 */ public class ZipCompressor { /** * 默认构造函数 */ public ZipCompressor() { } /** * @param targetPath 目的压缩文件保存路径 * @return void * @throws Exception * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip */ 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 ="" ; if(resourcesFile.getName().indexOf(".")!=-1){ targetName = resourcesFile.getName().substring(0,resourcesFile.getName().indexOf("."))+".zip"; //如果是文件则取文件名字 }else{ targetName = resourcesFile.getName() + ".zip"; //目的压缩文件名 } FileOutputStream outputStream = new FileOutputStream(targetPath + "\\" + targetName); CheckedOutputStream cos = new CheckedOutputStream(outputStream, new CRC32()); ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(cos); createCompressedFile(out, resourcesFile, ""); out.close(); } /** * @param out 输出流 * @param file 目标文件 * @return void * @throws Exception * @desc 生成压缩文件。 * 如果是文件夹,则使用递归,进行文件遍历、压缩 * 如果是文件,直接压缩 */ 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 { //当前的是文件,打包处理 //文件输入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(dir + file.getName()); out.putNextEntry(entry); // out.putNextEntry(new ZipEntry(dir)); //进行写操作 int j = 0; byte[] buffer = new byte[1024]; while ((j = bis.read(buffer)) > 0) { out.write(buffer, 0, j); } //关闭输入流 bis.close(); } } }
public class TestZip { public static void main(String args[]){ ZipCompressor zipCompressor = new ZipCompressor(); try { //把 txt 文件夹下的内容压缩到 zip文件夹下 生成的文件名为 txt.zip zipCompressor.compressedFile("F:\\test\\txt\\aaa.txt", "F:\\test\\zip"); System.out.println("压缩文件已经生成..."); } catch (Exception e) { System.out.println("压缩文件生成失败..."); e.printStackTrace(); } } }
相关文章推荐
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- 将指定文件/文件夹压缩成zip、rar压缩文件
- java把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹
- java实现zip压缩文件/文件夹
- java压缩及解压(.zip)文件文件夹
- java实现压缩指定文件夹(文件夹包含子文件夹或文件)为zip格式压缩包
- java zip和rar压缩文件的解压
- java实现zip压缩文件/文件夹
- java实现zip压缩文件/文件夹
- java.util.zip包无法正常解压rar软件压缩的rar文件
- Dream------Java--ant zip 对压缩文件进行指定位置的修改
- java 把指定文件夹下的所有文件目录和文件都压缩到指定文件夹下
- java将文件夹中的多个文件(包含子文件夹)压缩成zip文件