当前位置: 移动技术网 > IT编程>移动开发>Android > Android中文件的压缩和解压缩实例代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

手机信息群发软件,9c8808,鬼吹灯之精绝古城19-21下载

使用场景

当我们在应用的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,0, bytes_read);
      }
      out.closeentry();
    } else {
      //列出目录中的所有文件
      file[]entries = fileordirectory.listfiles();
      for (int i= 0; i < entries.length;i++) {
        //递归压缩
        zipfileordirectory(out,entries,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,0, 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,0, len);
    }
  }finally{
    close(zis);
    close(fos);
  }
}

以上所述是小编给大家介绍的android中文件的压缩和解压缩实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网