我就废话不多说了,大家还是直接看代码吧~

不多说,直接上代码

public static void main(string[] args) throws exception{
    
    //压缩文件
    file src = new file("e:/xx/aa.txt");
    file zipfile = new file("e:/xx/a.zip");
    fileoutputstream fos = new fileoutputstream(zipfile);
    zipoutputstream zos = new zipoutputstream(fos);
    bufferedinputstream bis = new bufferedinputstream(new fileinputstream(src));
    zipentry entry = new zipentry( src.getname());
    zos.putnextentry(entry);
    int count;
    byte[] buf = new byte[1024];
    while ((count = bis.read(buf)) != -1) {
      zos.write(buf, 0, count);
    }
    bis.close();
    //fos.close();
    zos.close();//
    }

压缩的步骤是:

src将要压缩的文件,zipfile 压缩后的文件,压缩流套接zipfile,然后将src文件写入zipfile,其中zipentry中放入的源文件的当前名称,putnextentry是将源文件的当前名称定位到条目数据的开始处。

补充:java zip压缩输入输出流

zip是一种较为常见的压缩形式,在java中要想实现zip的压缩需要导入java.util.zip包,可以使用此包中的zipfile、zipoutputstream、zipinputstream、zipentry几个类完成。

zipoutputstream类的常用方法

zipinputstream类的常用方法

在java io中,不仅可以实现zip压缩格式的输入、输出,也可以实现jar及gzip文件格式的压缩:

1、jar压缩的支持类保存在java.util.jar包中,常用的类有 jaroutputstream(jar压缩输出流)、jarinputstream(jar压缩输入流)、jarfile(jar文件)、jarentry(jar实体)

2、gzip是用于unix系统的文件压缩,在linux中经常会使用到*.gz的文件,就是gzip格式,gzip压缩的支持类保存在java.util.zip包中,常用的类有 gzipoutputstream(gzip压缩输出流)、gzipinputstream(gzip压缩输入流)

注意:

1、压缩文件中的每一个压缩实体都使用zipentry保存,一个压缩文件中可能包含一个或多个zipentry对象。

2、在java中可以进行zip、jar、gz三种格式的压缩支持,操作流程基本上是一致的。

3、zipoutputstream可以进行压缩的输出,但是输出的位置不一定是文件。

4、zipfile表示每一个压缩文件,可以得到每一个压缩实体的输入流。

压缩文件

import java.io.*;
import java.util.zip.*; 
public class myzip { // 创建类
 private void zip(string zipfilename, file inputfile) throws exception {
 zipoutputstream out = new zipoutputstream(new fileoutputstream(
  zipfilename)); // 创建zipoutputstream类对象
 zip(out, inputfile, ""); // 调用方法
 system.out.println("压缩中…"); // 输出信息
 out.close(); // 将流关闭
 }
 
 private void zip(zipoutputstream out, file f, string base)
  throws exception { // 方法重载
 if (f.isdirectory()) { // 测试此抽象路径名表示的文件是否是一个目录
  file[] fl = f.listfiles(); // 获取路径数组
  out.putnextentry(new zipentry(base + "/")); // 写入此目录的entry
  base = base.length() == 0 ? "" : base + "/"; // 判断参数是否为空
  for (int i = 0; i < fl.length; i++) { // 循环遍历数组中文件
  zip(out, fl[i], base + fl[i]);
  }
 } else {
  out.putnextentry(new zipentry(base)); // 创建新的进入点
  // 创建fileinputstream对象
  fileinputstream in = new fileinputstream(f);
  int b; // 定义int型变量
  system.out.println(base);
  while ((b = in.read()) != -1) { // 如果没有到达流的尾部
  out.write(b); // 将字节写入当前zip条目
  }
  in.close(); // 关闭流
 }
 }
 
 public static void main(string[] temp) { // 主方法
 myzip book = new myzip(); // 创建本例对象
 try {
  // 调用方法,参数为压缩后文件与要压缩文件
  book.zip("hello.zip", new file("src"));
  system.out.println("压缩完成"); // 输出信息
 } catch (exception ex) {
  ex.printstacktrace();
 }
 }
}

解压文件

import java.io.*;
import java.util.zip.*;
 
public class decompressing { // 创建文件
 public static void main(string[] temp) {
 zipinputstream zin; // 创建zipinputstream对象
 try { // try语句捕获可能发生的异常
  zin = new zipinputstream(new fileinputstream("hello.zip"));
  // 实例化对象,指明要进行解压的文件
  zipentry entry = zin.getnextentry(); // 获取下一个zipentry
  while (((entry = zin.getnextentry()) != null)
   && !entry.isdirectory()) {
  // 如果entry不为空,并不在同一目录下
  file file = new file("d:\\" + entry.getname()); // 获取文件目录
  system.out.println(file);
  if (!file.exists()) { // 如果该文件不存在
   file.mkdirs();// 创建文件所在文件夹
   file.createnewfile(); // 创建文件
  }
  zin.closeentry(); // 关闭当前entry
  system.out.println(entry.getname() + "解压成功");
  }
  zin.close(); // 关闭流
 } catch (exception e) {
  e.printstacktrace();
 }
 }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。