Android中文件的压缩和解压缩实例代码

Android中文件的压缩和解压缩实例代码,第1张

概述使用场景当我们在应用的Assets目录中需要加入文件时,可以直接将源文件放入,但这样会造成打包后的apk整体过大,此时就需要将放入的文件进行压缩.又如当我们需要从服务器中下载文件时,如果下载源文件耗时又消耗流量,较

使用场景

当我们在应用的Assets目录中需要加入文件时,可以直接将源文件放入,但这样会造成打包后的apk整体过大,此时就需要将放入的文件进行压缩.又如当我们需要从服务器中下载文件时,如果下载源文件耗时又消耗流量,较大文件需要压缩,可以使得传输效率大大提高.下面我们就学习下基本的文件压缩和解压缩.Java中提供了压缩和解压缩的输入输出流

public static voID zip(String src,String dest) throwsIOException {  //定义压缩输出流  ZipOutputStreamout = null;  try {    //传入源文件    file outfile= newfile(dest);    file fileOrDirectory= newfile(src);    //传入压缩输出流    out = newZipOutputStream(newfileOutputStream(outfile));    //判断是否是一个文件或目录    //如果是文件则压缩    if (fileOrDirectory.isfile()){      zipfileOrDirectory(out,fileOrDirectory,"");    } else {      //否则列出目录中的所有文件递归进行压缩      file[]entrIEs = fileOrDirectory.Listfiles();      for (int i= 0; i < entrIEs.length;i++) {        zipfileOrDirectory(out,entrIEs,"");      }    }  }catch(IOException ex) {    ex.printstacktrace();  }finally{    if (out!= null){      try {        out.close();      }catch(IOException ex) {        ex.printstacktrace();      }    }  }}private static voID zipfileOrDirectory(ZipOutputStream out,file fileOrDirectory,String curPath)throwsIOException {  fileinputStreamin = null;  try {    //判断目录是否为null    if (!fileOrDirectory.isDirectory()){      byte[] buffer= new byte[4096];      int bytes_read;      in= newfileinputStream(fileOrDirectory);      //归档压缩目录      ZipEntryentry = newZipEntry(curPath + fileOrDirectory.getname());      //将压缩目录写到输出流中      out.putNextEntry(entry);      while ((bytes_read= in.read(buffer))!= -1) {        out.write(buffer,bytes_read);      }      out.closeEntry();    } else {      //列出目录中的所有文件      file[]entrIEs = fileOrDirectory.Listfiles();      for (int i= 0; i < entrIEs.length;i++) {        //递归压缩        zipfileOrDirectory(out,curPath + fileOrDirectory.getname()+ "/");      }    }  }catch(IOException ex) {    ex.printstacktrace();  }finally{    if (in!= null){      try {        in.close();      }catch(IOException ex) {        ex.printstacktrace();      }    }  }}

上述代码存在问题,若文件压缩后仍然很大怎么办,换句话说文件压缩率低也是问题,java中也专门对linux提供了高压缩率的输入输出流,其使用方法和上述代码相似.高压缩率输入输出流:(GZIPinputStream和GZIPOutputStream)

文件压缩

public static voID zip(file srcfile,file desfile)throwsIOException {  GZIPOutputStreamzos = null;  fileinputStreamfis = null;  try {    //创建压缩输出流,将目标文件传入    zos = newGZIPOutputStream(newfileOutputStream(desfile));    //创建文件输入流,将源文件传入    fis = newfileinputStream(srcfile);    byte[] buffer= new byte[1024];    int len= -1;    //利用IO流写入写出的形式将源文件写入到目标文件中进行压缩    while ((len= (fis.read(buffer)))!= -1) {      zos.write(buffer,len);    }  }finally{    close(zos);    close(fis);  }}

文件解压缩

public static voID unZip(file srcfile,file desfile) throws IOException {  GZIPinputStream zis= null;  fileOutputStreamfos = null;  try {    //创建压缩输入流,传入源文件    zis = new GZIPinputStream(newfileinputStream(srcfile));    //创建文件输出流,传入目标文件    fos = newfileOutputStream(desfile);    byte[] buffer= new byte[1024];    int len= -1;    //利用IO流写入写出的形式将压缩源文件解压到目标文件中    while ((len= (zis.read(buffer)))!= -1) {      fos.write(buffer,len);    }  }finally{    close(zis);    close(fos);  }}

以上所述是小编给大家介绍的AndroID中文件的压缩和解压缩实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android中文件的压缩和解压缩实例代码全部内容,希望文章能够帮你解决Android中文件的压缩和解压缩实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146763.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存