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. java8--Stream的flatmap与map异同的理解

    大纲: 异同点 示例 一.异同点 他们的相同点是接收的入参都是一个function. 不同点这个入参function的返回不同.map返回一个对象,flatmap返回一个stream. 这就使得map ...

  2. day27 模块:正则re, configparser, subprocess

    Python之路,Day15 = Python基础15 re 模块补充 ret = re.findall("c.d", "abc\nd", re.S) # 后面 ...

  3. teb_local_planner安装及使用

    teb_local_planner的详尽资料(包括安装及导航,参数调节等)请参考其ros官方文档:http://wiki.ros.org/teb_local_planner和http://wiki.r ...

  4. System.Web.Mvc.ActionResult.cs

    ylbtech-System.Web.Mvc.ActionResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Publi ...

  5. 2017/8/4 SCJP学习

    2 Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . 85 Encapsulation (Exam Object ...

  6. PHP面向对象之继承的基本思想

    图例 概念和说明 代码展示 <?php header('content-type:text/html;charset=utf-8'); //学生考试系统 class Student{ publi ...

  7. element表单验证

    rules: { name:[{ required: true, message: '请输入用户名', trigger: 'blur' },{ min: 2, max: 5, message: '长度 ...

  8. PCA降维的前世今生

    PCA降维的数学原理 PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分 ...

  9. 新安装一个eclipse,导入一个web项目,右键点击项目选择Properties,找不到project facets和Server选项。

    解决方式: 1.点击:eclipse导航栏中点击Help->Install New Software 2.点击Add添加 3在弹出框中填写以下信息 name:keep(名字随便取) locati ...

  10. php访问其他网站接口

    使用函数:  file_get_contents($url); 传入接口url及其参数:如 $url="http://192.168.1.1/test.jsp?id=1&type=2 ...