笔记101 | 文件的压缩与解压笔记
时间:2022-07-22
本文章向大家介绍笔记101 | 文件的压缩与解压笔记,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1.解压
1.1读取压缩文件
bean
public class Logo {
public String mName;
public String mPath;
public Bitmap mBitmap32;
public int pi;
}
主要逻辑方法
private void loadZip() {
// get a zip file instance
File file = new File("/system/etc/freq_logo.zip");
if(file.canRead() && file.isFile()) {
try {
// create a ZipInputStream instance
ZipInputStream is = null;
try {
is = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
// create a ZipEntry instance , lay the every file from
// decompress file temporarily
ZipEntry entry = null;
// a circle to get every file
while((entry = is.getNextEntry()) != null) {
addFile("/system/etc/freq_logo.zip", entry.getName());
}
} catch (Exception e) {
} finally {
if(is != null) {
is.close();
}
}
} catch (Exception e) {
}
}
}
private void addFile(String path, String name) {
// create a AsyncTask addFile
new AddFileTask().execute(path, name);
}
private class AddFileTask extends AsyncTask<String, Integer, Logo> {
@Override
protected Logo doInBackground(String... params) {
try {
String path = params[0];
String name = params[1];
Logo l = new Logo();
if(name != null && name.length() > 0) {
l.mName = name;
l.pi = Integer.parseInt(name.substring(15, 19));
}
l.mPath = path;
return l;
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Logo result) {
if(result == null) return;
Log.i("md",result.mName+ " "+result.mPath+" "+result.pi+" "+result.mBitmap32);
mLogo.add(result); //add result data
}
}
由于考虑到文件数量,所以使用的是AddFileTask异步读取 此逻辑并没有把zip文件解压出来,而是以读文件的形式去获取内容,保存到mLogo 打印如下
2020-07-10 16:46:38.325 2274-2274/com.tw.radio I/md: freq_logo/logo_1024.jpg /system/etc/freq_logo.zip 1024 null
2020-07-10 16:46:38.325 2274-2274/com.tw.radio I/md: freq_logo/logo_1028.jpg /system/etc/freq_logo.zip 1028 null
2020-07-10 16:46:38.326 2274-2274/com.tw.radio I/md: freq_logo/logo_1054.jpg /system/etc/freq_logo.zip 1054 null
1.2解压文件
如果要实现解压成文件,可以参考以下逻辑
public void TestZipInputStream() throws IOException {
// get a zip file instance
File file = new File ( "/system/etc/freq_logo.zip" ) ;
// get a ZipFile instance
ZipFile zipFile = new ZipFile ( file ) ;
// create a ZipInputStream instance
ZipInputStream zis = new ZipInputStream ( new FileInputStream (
file ) ) ;
// create a ZipEntry instance , lay the every file from
// decompress file temporarily
ZipEntry entry = null ;
// a circle to get every file
while ( ( entry = zis.getNextEntry ( ) ) != null )
{
// define the path to set the file
File outFile = new File ( "/data/tw/freq_logo/"
+ entry.getName ( ) ) ;
// if the file's parent directory wasn't exits ,than
// create the directory
if ( !outFile.getParentFile ().exists ()){
outFile.getParentFile ().mkdir () ;
}
// if the file not exits ,than create the file
if (!outFile.exists ()){
outFile.createNewFile ();
}
// create an input stream
BufferedInputStream bis = new BufferedInputStream (zipFile.getInputStream (entry)) ;
// create an output stream
BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream(outFile)) ;
byte [] b = new byte [100] ;
while (true) {
int len = bis.read (b) ;
if (len == - 1)
break ;
bos.write (b , 0 , len) ;
}
// close stream
bis.close ( ) ;
bos.close ( ) ;
}
zis.close ( ) ;
}
获取的文件如下
image.png
2.压缩
2.1文件的压缩:
public class TestFile
{
public static void main ( String [ ] args ) throws IOException
{
// new a file input stream
FileInputStream fis = new FileInputStream (
"/home/liangruihua/ziptest/1.txt" ) ;
BufferedInputStream bis = new BufferedInputStream ( fis ) ;
// new a zipPutputStream
// /home/liangruihua/ziptest/1.zip -- the out put file path and
// name
ZipOutputStream zos = new ZipOutputStream (
new FileOutputStream (
"/home/liangruihua/ziptest/1.zip" ) ) ;
BufferedOutputStream bos = new BufferedOutputStream ( zos ) ;
// set the file name in the .zip file
zos.putNextEntry ( new ZipEntry ( "1.txt" ) ) ;
// set the declear
zos.setComment ( "by liangruihua test!" ) ;
byte [ ] b = new byte [1024] ;
int ch = 0;
while ( true )
{
int len = bis.read ( b ) ;
if ( len == - 1 )
break ;
bos.write ( b , 0 , len ) ;
}
bos.flush();
}
2.2文件夹的压缩
public class TestDir
{
public static void main ( String [ ] args ) throws IOException
{
// the file path need to compress
File file = new File ( "/home/liangruihua/ziptest/test" ) ;
ZipOutputStream zos = new ZipOutputStream (
new FileOutputStream (
"/home/liangruihua/ziptest/test.zip" ) ) ;
// judge the file is the directory
if ( file.isDirectory ( ) )
{
// get the every file in the directory
File [ ] files = file.listFiles ( ) ;
for ( int i = 0 ; i < files.length ; i ++ )
{
// new the BuuferedInputStream
BufferedInputStream bis = new BufferedInputStream (
new FileInputStream (
files [ i ] ) ) ;
// the file entry ,set the file name in the zip
// file
zos.putNextEntry ( new ZipEntry ( file
.getName ( )
+ file.separator
+ files [ i ].getName ( ) ) ) ;
while ( true )
{
byte [ ] b = new byte [ 100 ] ;
int len = bis.read ( b ) ;
if ( len == - 1 )
break ;
zos.write ( b , 0 , len ) ;
}
// close the input stream
bis.close ( ) ;
}
}
// close the zip output stream
zos.close ( ) ;
}
}
- 【干货】对抗自编码器PyTorch手把手实战系列——PyTorch实现对抗自编码器
- Selenium2+python自动化23-富文本(自动发帖)
- 2-Sat+输出可行解(个人模版)
- 协同过滤原理及Python实现
- 每周学点大数据 | No.25二叉搜索树回顾(二)
- RBF神经网络及Python实现(附源码)
- 【干货】计算机视觉实战系列03——用Python做图像处理
- Adaboost从原理到实现(Python)
- Selenium+python自动化22-发送各种类型附件邮件
- Selenium2+python自动化38-显示等待(WebDriverWait)
- 逆元(个人模版)
- Selenium2+python自动化37-爬页面源码(page_source)
- ex_gcd(个人模版)
- Selenium2+python自动化36-判断元素存在
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 我服务又双叒叕奔溃了,含泪干货分享
- Azure DevOps+Docker+Asp.NET Core 实现CI/CD(一 .简介与创建自己的代理池)
- Android数据库高手秘籍(十二),LitePal的索引功能
- 使用pm2管理go应用进程
- 用Docker搭建Redis主从复制的集群
- IDEA 非常重要的一些设置项 → 一连串的问题差点让我重新用回 Eclipse !
- ArrayList源码分析(基于jdk1.8)(二):subList陷阱补充
- Windows10中安装Docker
- Windows下Docker安装ClickHouse
- ArrayList源码分析(基于jdk1.8)(三):Arrays.asList方法带来的问题
- 对基本类型包装类常量池的补充
- 与IntegerCache有关的一个比较坑的面试题
- C# Foreach循环本质与枚举器
- Java中的时间和日期(一):有关java时间的哪些坑
- Java中的时间和日期(二):java时间存储的基本原理