笔记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 ( ) ;
}
}
- 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 数组属性和方法
- 详解Android activity与fragment之间的通信交互
- Android App内监听截图加二维码功能代码
- 微信小程序登录与注册验证码倒计时的效果实现
- Android仿微信朋友圈全文、收起功能的实例代码
- TextView实现图文混合编排的方法
- 小程序云函数调用http或https请求外部数据
- 详解Android中图片的三级缓存及实例
- 借助云开发实现小程序的登陆注册功能
- TextView显示文本控件两种方法 TextView显示link的方法
- Java入门005~Springboot2.2.4引入freemarker模板
- TabLayout使用方法详解
- Java入门006~springboot+freemarker+bootstrap快速实现管理后台
- Java入门007~springboot+freemarker+bootstrap快速实现分页功能
- ToolBar使用方法详解
- Android 中WallpaperManager用法实例