java将有关zip压缩的内容都封装在java.util.zip宝中,用java实现zip压缩,不用考虑压缩算法,java已经将这些进行了封装

实际上用java实现zip压缩涉及的就是一个“输入输出流”的概念

用java实现一个文件的zip压缩,过程可以简单地表示为:

当然具体实现要比这个复杂一点,比如要先像zip文件中写入目录进入点。。如果要压缩文件夹中的内容要遍历文件夹中的文件和子文件夹。


/**
* @author: hxp
* @date: 2019/3/30 18:09
* @description:zip压缩工具
*/
public class ZipCompress {
/**
* 目的地Zip文件
*/
private String zipFileName;
/**
* 源文件(待压缩的文件或文件夹)
*/
private String sourceFileName; public ZipCompress(String zipFileName,String sourceFileName)
{
this.zipFileName=zipFileName;
this.sourceFileName=sourceFileName;
} public void zip() throws Exception
{
System.out.println("压缩中...");
File file = new File(zipFileName);
if(!file.exists()){
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
}
//创建zip输出流
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); //创建缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(out); File sourceFile = new File(sourceFileName); //调用函数
compress(out,bos,sourceFile,sourceFile.getName()); bos.close();
out.close();
System.out.println("压缩完成"); } public void compress(ZipOutputStream out,BufferedOutputStream bos,File sourceFile,String base) throws Exception
{
//如果路径为目录(文件夹)
if(sourceFile.isDirectory())
{ //取出文件夹中的文件(或子文件夹)
File[] list = sourceFile.listFiles(); //如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
if(list.length==0)
{
System.out.println(base+"/");
out.putNextEntry( new ZipEntry(base+"/") );
}
else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
{
for(int i=0;i<list.length;i++)
{
compress(out,bos,list[i],base+"/"+list[i].getName());
}
}
}
else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
{
out.putNextEntry( new ZipEntry(base) );
FileInputStream fos = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fos); int tag;
System.out.println(base);
//将源文件写入到zip文件中
while((tag=bis.read())!=-1)
{
bos.write(tag);
}
bis.close();
fos.close(); }
}
}
 

zip压缩工具类的更多相关文章

  1. 【C#】依赖于SharpZipLib的Zip压缩工具类

    上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib  vers ...

  2. 最近工作用到压缩,写一个zip压缩工具类

    package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...

  3. 压缩工具类 - ZipUtils.java

    压缩工具类,提供压缩文件.解压文件的方法. 源码如下:(点击下载 - ZipUtils.java .FolderUtils.java.ant-1.7.0.jar.commons-io-2.4.jar. ...

  4. Java 实现文件压缩工具类

    package com.wdxc.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileI ...

  5. php ZIP压缩类实例分享

    php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...

  6. zip压缩工具 tar打包 打包并压缩

    6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩 zip压缩工具 xz,bzip2,gzip都不支持压缩目录 zip可以压缩目录 压缩文件 zip  2.txt.zip  2.txt [ ...

  7. zip压缩工具,unzip解压缩工具

    zip压缩工具,unzip解压缩工具=================== [root@aminglinux tmp]# yum install -y zip[root@aminglinux tmp] ...

  8. Zip压缩工具、tar打包、打包并压缩

    第5周第2次课(4月17日) 课程内容: 6.5 zip压缩工具6.6 tar打包6.7 打包并压缩 6.5 zip压缩工具 Zip压缩工具最大的特点就是可以支持压缩目录,也能够压缩文件,Window ...

  9. Linux centosVMware zip压缩工具、tar打包、打包并压缩

    一. zip压缩工具 可以用来压缩文件和目录,压缩目录是需要指定目录下的文件. [root@davery tmp]# cp 1.txt davery/[root@davery tmp]# du -sh ...

随机推荐

  1. node---处理get请求

    const http=require('http') const querystring=require('querystring') const server = http.createServer ...

  2. js 高级 原型与原型链

    * 所有函数都有一个特别的属性: * `prototype` : 显式原型属性* 所有实例对象都有一个特别的属性: * `__proto__` : 隐式原型属性 1.  每个函数都有一个prototy ...

  3. APB简介

    一.血缘 AMBA: Advanced Microcontroller Bus Architecture 高级处理器总线架构 AHB: Advanced High-performance Bus 高级 ...

  4. thinkphp 模板继承

    模板继承是一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类的继承一样,模板也可以定义一个基础模板(或者是布局),并且其中定义相关的区 ...

  5. springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发

    工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...

  6. LUOGU P4253 [SCOI2015]小凸玩密室(树形dp)

    传送门 解题思路 玄学树形\(dp\),题目描述极其混乱...看错了两次题,设首先根据每次必须点完子树里的灯才能点别的,那么点灯情况只有两种,第一种是点到某一个祖先,第二种是点到某一个祖先的兄弟.所以 ...

  7. VS2010-MFC(对话框:消息对话框)

    转自:http://www.jizhuomi.com/software/171.html 前面几节讲了属性页对话框,我们可以根据所讲内容方便的建立自己的属性页对话框.本节讲解Windows系统中最常用 ...

  8. Ubuntu 上如何安装Docker

    Docker 是一个开源项目,为开发者和系统管理员提供了一个开放的平台,在任何地方通过打包和运行应用程序作为一个轻量级的容器.Docker 在软件容器内自动部署应用程序.Docker 最开始由 Sol ...

  9. ERROR 1839 (HY000): @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_MODE = ON

    从cdb上dump一个库结构,准备与本地结构做对比(可以直接compare,但速度贼慢).使用dump脚本在本地创建的时候报错 -- 导出指定库的结构 shell> mysqldump -hxx ...

  10. C# SQL 多条件查询技巧

    #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneN ...