java实现文件的压缩和解压
代码压缩实现
package com.hjh.demo.zip;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
//使用org.apache.tools.zip这个就不会中文乱码 来自于ant.jar
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
//使用java.util.zip原生ZipOutputStream与ZipEntry会中文乱码
//import java.util.zip.ZipOutputStream;
//import java.util.zip.ZipEntry;
public class TestZip
{
static String filePath = "F:/test/json.txt";//需要压缩的文件夹完整路径
static String fileName = "zip";//需要压缩的文件夹名
static String outPath = "F:/test/zip/Test.zip";//压缩完成后保存为Test.zip文件,名字随意
public static void main(String args[]) throws Exception
{
OutputStream is = new FileOutputStream(outPath);//创建Test.zip文件
CheckedOutputStream cos = new CheckedOutputStream(is, new CRC32());//检查输出流,采用CRC32算法,保证文件的一致性
ZipOutputStream zos = new ZipOutputStream(cos);//创建zip文件的输出流
zos.setEncoding("GBK");//设置编码,防止中文乱码
File file = new File(filePath);//需要压缩的文件或文件夹对象
// ZipFile(zos,file);//压缩文件的具体实现函数
zipFileContent(zos,file);
zos.close();
cos.close();
is.close();
System.out.println("压缩完成");
}
public static void zipFileContent(ZipOutputStream zos,File file) throws Exception
{
if(file.isDirectory())
{
for(File f : file.listFiles())
{
zipFileContent(zos, f);
}
}
else
{
//用字节方式读取源文件
InputStream is = new FileInputStream(file.getPath());
//创建一个缓存区
BufferedInputStream bis = new BufferedInputStream(is);
//字节数组,每次读取1024个字节
byte [] b = new byte[1024];
zos.putNextEntry(new ZipEntry(file.getName()));
//循环读取,边读边写
while(bis.read(b) != -1)
{
zos.write(b);//写入压缩文件
}
//关闭流
bis.close();
is.close();
}
}
//递归,获取需要压缩的文件夹下面的所有子文件,然后创建对应目录与文件,对文件进行压缩
public static void ZipFile(ZipOutputStream zos,File file) throws Exception
{
if(file.isDirectory())
{
//创建压缩文件的目录结构
zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))+File.separator));
for(File f : file.listFiles())
{
ZipFile(zos,f);
}
}
else
{
//打印输出正在压缩的文件
System.out.println("正在压缩文件:"+file.getName());
//创建压缩文件
zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))));
//用字节方式读取源文件
InputStream is = new FileInputStream(file.getPath());
//创建一个缓存区
BufferedInputStream bis = new BufferedInputStream(is);
//字节数组,每次读取1024个字节
byte [] b = new byte[1024];
//循环读取,边读边写
while(bis.read(b)!=-1)
{
zos.write(b);//写入压缩文件
}
//关闭流
bis.close();
is.close();
}
}
}
代码解压实现
package com.hjh.demo.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
//使用org.apache.tools.zip这个就不会中文乱码 来自于ant.jar
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
//使用java.util.zip原生ZipOutputStream与ZipEntry会中文乱码
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
public class TestUnzip
{
static String zipPath = "F:/test/zip/Test.zip";//需要解压的压缩文件
static String outPath = "F:/test/zip/unzip/";//解压完成后保存路径,记得"\\"结尾哈
public static void main(String args[]) throws Exception
{
ZipFile zipFile = new ZipFile(zipPath,"GBK");//压缩文件的实列,并设置编码
//获取压缩文中的所以项
for(Enumeration<ZipEntry> enumeration = zipFile.getEntries();enumeration.hasMoreElements();)
{
ZipEntry zipEntry = enumeration.nextElement();//获取元素
System.out.println("解压到的条目 : " + zipEntry.getName());
//排除空文件夹
if(!zipEntry.getName().endsWith(File.separator))
{
// System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息
OutputStream os = new FileOutputStream(outPath+zipEntry.getName());//创建解压后的文件
BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流
InputStream is = zipFile.getInputStream(zipEntry);//读取元素
BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流
CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保证文件的一致性
byte [] b = new byte[1024];//字节数组,每次读取1024个字节
//循环读取压缩文件的值
while(cos.read(b)!=-1)
{
bos.write(b);//写入到新文件
}
cos.close();
bis.close();
is.close();
bos.close();
os.close();
}
else
{
//如果为空文件夹,则创建该文件夹
new File(outPath+zipEntry.getName()).mkdirs();
}
}
System.out.println("解压完成");
zipFile.close();
}
}
java实现文件的压缩和解压的更多相关文章
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...
- linux下文件加密压缩和解压的方法
一.用tar命令 对文件加密压缩和解压 压缩:tar -zcf - filename |openssl des3 -salt -k password | dd of=filename.des3 此命 ...
- [Swift通天遁地]七、数据与安全-(9)文件的压缩和解压
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 用jdk1.6的pack200和unpack200,对jar文件进行压缩和解压 .pack.gz
用jdk1.6的pack200和unpack200,对jar文件进行压缩和解压 解压xxx.jar.pack.gz为xxx.jar:unpack200 -r xxx.jar.pack.gz xxx.j ...
- C#文件或文件夹压缩和解压
C#文件或文件夹压缩和解压方法有很多,本文通过使用ICSharpCode.SharpZipLib.dll来进行压缩解压 1.新建一个winform项目,选择项目右键 管理NuGet程序包,搜索ICSh ...
- gz文件的压缩和解压
gz文件的压缩和解压 压缩: gzip filename 解压: gunzip filename.gz
- ZIP文件流压缩和解压
前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...
- 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货
关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩类博客,压缩的是zip文件htt ...
- 利用c#自带的类对文件进行压缩和解压处理
在做网络传输文件的小例子的时候,当传输的文件比较大的时候,我们通常都是将文件经过压缩之后才进行传输,以前都是利用第三方插件来对文件进行压缩的,但是现在我发现了c#自带的类库也能够实现文件的压缩,实际上 ...
随机推荐
- virtualenv使用说明
创建虚拟环境virtualenv [虚拟环境名称] 如:virtualenv ENV 启动虚拟环境 cd ENV source ./bin/activate 注意此时命令行会多一个(ENV),ENV为 ...
- android自定义View_4——自定义属性的格式选择
reference - if it references another resource id (e.g, "@color/my_color", "@layout/my ...
- windows 32位以及64位的inline hook
Tips : 这篇文章的主题是x86及x64 windows系统下的inline hook实现部分. 32位inline hook 对于系统API的hook,windows 系统为了达成hotpatc ...
- lumen 获得当前uri 如/xxx/{id}
因为想实现通过url判断是否有权限,所有需要拿到当前的route方法的name,如下 $api->get('role/grant/{id}', 'RoleController@getGrant' ...
- centos7下挂载U盘和移动硬盘
挂载U盘 1.使用fdisk -l命令查看磁盘情况 [root@localhost ~]# fdisk -l 磁盘 /dev/sda:1000.2 GB, 1000204886016 字节,19535 ...
- mysql 集群的一些概念
读写分离: 主备机有 master-master方式,mysql自己提供两个机器之间的备份 binlog方式,一个机器master 用于写数据,一个用于读数据,写数据的那个机器也应有读读功能,有既有读 ...
- windows解除端口占用
1.步骤如图: 图片原文:https://stackoverflow.com/questions/33108185/intellij-keeps-displaying-annoying-message ...
- 【转】Python 30个实用小Tips
1. 原地交换两个数字 Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例: x, y = 10, 20 print(x, y) x, y = y, x print ...
- 斯坦福大学Andrew Ng - 机器学习笔记(3) -- 神经网络模型
大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...
- 爬取豆瓣电影信息保存到Excel
from bs4 import BeautifulSoup import requests import html.parser from openpyxl import Workbook,load_ ...