欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现zip文件压缩及解压缩的方法

程序员文章站 2022-06-29 09:19:21
本文实例讲述了android实现zip文件压缩及解压缩的方法。分享给大家供大家参考。具体如下: dirtraversal.java如下: package com...

本文实例讲述了android实现zip文件压缩及解压缩的方法。分享给大家供大家参考。具体如下:

dirtraversal.java如下:

package com.once;
import java.io.file;
import java.util.arraylist;
import java.util.linkedlist;
/**
 * 文件夹遍历
 * @author once
 *
 */
public class dirtraversal {
  //no recursion
  public static linkedlist<file> listlinkedfiles(string strpath) {
    linkedlist<file> list = new linkedlist<file>();
    file dir = new file(strpath);
    file file[] = dir.listfiles();
    for (int i = 0; i < file.length; i++) {
      if (file[i].isdirectory())
        list.add(file[i]);
      else
        system.out.println(file[i].getabsolutepath());
    }
    file tmp;
    while (!list.isempty()) {
      tmp = (file) list.removefirst();
      if (tmp.isdirectory()) {
        file = tmp.listfiles();
        if (file == null)
          continue;
        for (int i = 0; i < file.length; i++) {
          if (file[i].isdirectory())
            list.add(file[i]);
          else
            system.out.println(file[i].getabsolutepath());
        }
      } else {
        system.out.println(tmp.getabsolutepath());
      }
    }
    return list;
  }
  //recursion
  public static arraylist<file> listfiles(string strpath) {
    return refreshfilelist(strpath);
  }
  public static arraylist<file> refreshfilelist(string strpath) {
    arraylist<file> filelist = new arraylist<file>();
    file dir = new file(strpath);
    file[] files = dir.listfiles();
    if (files == null)
      return null;
    for (int i = 0; i < files.length; i++) {
      if (files[i].isdirectory()) {
        refreshfilelist(files[i].getabsolutepath());
      } else {
        if(files[i].getname().tolowercase().endswith("zip"))
          filelist.add(files[i]);
      }
    }
    return filelist;
  }
}

ziputils.java如下:

package com.once;
import java.io.*;
import java.util.arraylist;
import java.util.collection;
import java.util.enumeration;
import java.util.zip.zipentry;
import java.util.zip.zipexception;
import java.util.zip.zipfile;
import java.util.zip.zipoutputstream;
/**
 * java utils 实现的zip工具
 *
 * @author once
 */
public class ziputils {
  private static final int buff_size = 1024 * 1024; // 1m byte
  /**
   * 批量压缩文件(夹)
   *
   * @param resfilelist 要压缩的文件(夹)列表
   * @param zipfile 生成的压缩文件
   * @throws ioexception 当压缩过程出错时抛出
   */
  public static void zipfiles(collection<file> resfilelist, file zipfile) throws ioexception {
    zipoutputstream zipout = new zipoutputstream(new bufferedoutputstream(new fileoutputstream(
        zipfile), buff_size));
    for (file resfile : resfilelist) {
      zipfile(resfile, zipout, "");
    }
    zipout.close();
  }
  /**
   * 批量压缩文件(夹)
   *
   * @param resfilelist 要压缩的文件(夹)列表
   * @param zipfile 生成的压缩文件
   * @param comment 压缩文件的注释
   * @throws ioexception 当压缩过程出错时抛出
   */
  public static void zipfiles(collection<file> resfilelist, file zipfile, string comment)
      throws ioexception {
    zipoutputstream zipout = new zipoutputstream(new bufferedoutputstream(new fileoutputstream(
        zipfile), buff_size));
    for (file resfile : resfilelist) {
      zipfile(resfile, zipout, "");
    }
    zipout.setcomment(comment);
    zipout.close();
  }
  /**
   * 解压缩一个文件
   *
   * @param zipfile 压缩文件
   * @param folderpath 解压缩的目标目录
   * @throws ioexception 当解压缩过程出错时抛出
   */
  public static void upzipfile(file zipfile, string folderpath) throws zipexception, ioexception {
    file desdir = new file(folderpath);
    if (!desdir.exists()) {
      desdir.mkdirs();
    }
    zipfile zf = new zipfile(zipfile);
    for (enumeration<?> entries = zf.entries(); entries.hasmoreelements();) {
      zipentry entry = ((zipentry)entries.nextelement());
      inputstream in = zf.getinputstream(entry);
      string str = folderpath + file.separator + entry.getname();
      str = new string(str.getbytes("8859_1"), "gb2312");
      file desfile = new file(str);
      if (!desfile.exists()) {
        file fileparentdir = desfile.getparentfile();
        if (!fileparentdir.exists()) {
          fileparentdir.mkdirs();
        }
        desfile.createnewfile();
      }
      outputstream out = new fileoutputstream(desfile);
      byte buffer[] = new byte[buff_size];
      int reallength;
      while ((reallength = in.read(buffer)) > 0) {
        out.write(buffer, 0, reallength);
      }
      in.close();
      out.close();
    }
  }
  /**
   * 解压文件名包含传入文字的文件
   *
   * @param zipfile 压缩文件
   * @param folderpath 目标文件夹
   * @param namecontains 传入的文件匹配名
   * @throws zipexception 压缩格式有误时抛出
   * @throws ioexception io错误时抛出
   */
  public static arraylist<file> upzipselectedfile(file zipfile, string folderpath,
      string namecontains) throws zipexception, ioexception {
    arraylist<file> filelist = new arraylist<file>();
 
    file desdir = new file(folderpath);
    if (!desdir.exists()) {
      desdir.mkdir();
    }
    zipfile zf = new zipfile(zipfile);
    for (enumeration<?> entries = zf.entries(); entries.hasmoreelements();) {
      zipentry entry = ((zipentry)entries.nextelement());
      if (entry.getname().contains(namecontains)) {
        inputstream in = zf.getinputstream(entry);
        string str = folderpath + file.separator + entry.getname();
        str = new string(str.getbytes("8859_1"), "gb2312");
        // str.getbytes("gb2312"),"8859_1" 输出
        // str.getbytes("8859_1"),"gb2312" 输入
        file desfile = new file(str);
        if (!desfile.exists()) {
          file fileparentdir = desfile.getparentfile();
          if (!fileparentdir.exists()) {
            fileparentdir.mkdirs();
          }
          desfile.createnewfile();
        }
        outputstream out = new fileoutputstream(desfile);
        byte buffer[] = new byte[buff_size];
        int reallength;
        while ((reallength = in.read(buffer)) > 0) {
          out.write(buffer, 0, reallength);
        }
        in.close();
        out.close();
        filelist.add(desfile);
      }
    }
    return filelist;
  }
  /**
   * 获得压缩文件内文件列表
   *
   * @param zipfile 压缩文件
   * @return 压缩文件内文件名称
   * @throws zipexception 压缩文件格式有误时抛出
   * @throws ioexception 当解压缩过程出错时抛出
   */
  public static arraylist<string> getentriesnames(file zipfile) throws zipexception, ioexception {
    arraylist<string> entrynames = new arraylist<string>();
    enumeration<?> entries = getentriesenumeration(zipfile);
    while (entries.hasmoreelements()) {
      zipentry entry = ((zipentry)entries.nextelement());
      entrynames.add(new string(getentryname(entry).getbytes("gb2312"), "8859_1"));
    }
    return entrynames;
  }
  /**
   * 获得压缩文件内压缩文件对象以取得其属性
   *
   * @param zipfile 压缩文件
   * @return 返回一个压缩文件列表
   * @throws zipexception 压缩文件格式有误时抛出
   * @throws ioexception io操作有误时抛出
   */
  public static enumeration<?> getentriesenumeration(file zipfile) throws zipexception,
      ioexception {
    zipfile zf = new zipfile(zipfile);
    return zf.entries();
  }
  /**
   * 取得压缩文件对象的注释
   *
   * @param entry 压缩文件对象
   * @return 压缩文件对象的注释
   * @throws unsupportedencodingexception
   */
  public static string getentrycomment(zipentry entry) throws unsupportedencodingexception {
    return new string(entry.getcomment().getbytes("gb2312"), "8859_1");
  }
  /**
   * 取得压缩文件对象的名称
   *
   * @param entry 压缩文件对象
   * @return 压缩文件对象的名称
   * @throws unsupportedencodingexception
   */
  public static string getentryname(zipentry entry) throws unsupportedencodingexception {
    return new string(entry.getname().getbytes("gb2312"), "8859_1");
  }
  /**
   * 压缩文件
   *
   * @param resfile 需要压缩的文件(夹)
   * @param zipout 压缩的目的文件
   * @param rootpath 压缩的文件路径
   * @throws filenotfoundexception 找不到文件时抛出
   * @throws ioexception 当压缩过程出错时抛出
   */
  private static void zipfile(file resfile, zipoutputstream zipout, string rootpath)
      throws filenotfoundexception, ioexception {
    rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : file.separator)
        + resfile.getname();
    rootpath = new string(rootpath.getbytes("8859_1"), "gb2312");
    if (resfile.isdirectory()) {
      file[] filelist = resfile.listfiles();
      for (file file : filelist) {
        zipfile(file, zipout, rootpath);
      }
    } else {
      byte buffer[] = new byte[buff_size];
      bufferedinputstream in = new bufferedinputstream(new fileinputstream(resfile),
          buff_size);
      zipout.putnextentry(new zipentry(rootpath));
      int reallength;
      while ((reallength = in.read(buffer)) != -1) {
        zipout.write(buffer, 0, reallength);
      }
      in.close();
      zipout.flush();
      zipout.closeentry();
    }
  }
}

希望本文所述对大家的android程序设计有所帮助。