java 压缩单个文件_java 实现压缩单个文件

先来实现一个简单的单文件压缩,主要是为了解一下压缩需要使用到的流。。

效果:

e4c8914883f19ebd4afcfc27a0e569dc.png

ee9eba262ed5e592cedf185ed8d134eb.png

说明:压缩实现使用ZipOutputStream

代码:

package com.gx.compress;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

* @ClassName: CompressUtil

* @Description: 压缩单个文件

* @author zhoujie

* @date 2018年7月29日 下午9:56:29

* @version V1.0

*/

public class CompressUtil {

static String path = "F:\\图片\\"; //文件夹路径

public static void main(String[] args) {

String filePath = path + "铜钱.jpg"; //图片名称

String outPath = path + "new.zip"; //压缩文件名称地址

zipUtil(filePath, outPath); //压缩

}

public static void zipUtil(String filePath, String outPath){

//输入

File file = null;

FileInputStream fis = null;

BufferedInputStream bin = null;

DataInputStream dis = null;

//输出

File outfile = null;

FileOutputStream fos = null;

BufferedOutputStream bos = null;

ZipOutputStream zos = null;

ZipEntry ze = null;

try {

//输入-获取数据

file = new File(filePath);

fis = new FileInputStream(file);

bin = new BufferedInputStream(fis);

dis = new DataInputStream(bin); //增强

//输出-写出数据

outfile = new File(outPath);

fos = new FileOutputStream(outfile);

bos = new BufferedOutputStream(fos, 1024); //the buffer size

zos = new ZipOutputStream(bos); //压缩输出流

ze = new ZipEntry(file.getName()); //实体ZipEntry保存

zos.putNextEntry(ze);

int len = 0;//临时文件

byte[] bts = new byte[1024]; //读取缓冲

while((len=dis.read(bts)) != -1){ //每次读取1024个字节

System.out.println(len);

zos.write(bts, 0, len); //每次写len长度数据,最后前一次都是1024,最后一次len长度

}

System.out.println("压缩成功");

} catch (Exception e) {

e.printStackTrace();

} finally{

try { //先实例化后关闭

zos.closeEntry();

zos.close();

bos.close();

fos.close();

dis.close();

bin.close();

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

以上仅为单个文件压缩,了解压缩流程。

下面这个是支持 单文件 或 文件夹 压缩:

ok。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用Zip类批量压缩多个文件,此前有一个压缩单个文件,也可参考,相关代码中可找到此源码。   public class ZipDemo extends JFrame{   JFileChooser fileChooser; //文件选择器   JList fileList; //待压缩文件列表   Vector files; //文件数据(待压缩文件)   JButton jbAdd; //增加文件按钮   JButton jbDelete; //删除文件按钮   JButton jbZip; //压缩按钮   JTextField target; //目标文件文本域   public ZipDemo(){   super("用ZIP压缩多个文件"); //调用父类构造函数   fileChooser=new JFileChooser(); //实例化文件选择器   files=new Vector(); //实例化文件数据Vector   fileList=new JList(files); //实例化已选择文件列表   jbAdd=new JButton("增加"); //实例化按钮组件   jbDelete=new JButton("删除");   jbZip=new JButton("压缩");   target=new JTextField(18);   JPanel panel=new JPanel(); //实例化面板,用于容纳按钮   panel.add(jbAdd); //增加组件到面板上   panel.add(jbDelete);   panel.add(jbZip);   JPanel panel2=new JPanel();   panel2.add(new JLabel("目标文件"));   panel2.add(target);   JScrollPane jsp=new JScrollPane(fileList);   Container container=getContentPane(); //得到容器   container.add(panel2,BorderLayout.NORTH); //增加组件到容器   container.add(jsp,BorderLayout.CENTER);   container.add(panel,BorderLayout.SOUTH);   jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //设置边界

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值