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

android数据存储之文件存储方法

程序员文章站 2024-02-29 18:14:10
文件存储是 android 中最基本的一种数据存储方式,它不对存储的内容进行任何的格式化处理,所有数据都是原封不动的保存到文件当中的。 概述 文件存取的核心就是输入...

文件存储是 android 中最基本的一种数据存储方式,它不对存储的内容进行任何的格式化处理,所有数据都是原封不动的保存到文件当中的。

概述

文件存取的核心就是输入流和输出流。

android文件的操作模式android数据存储之文件存储方法
文件的相关操作方法android数据存储之文件存储方法

文件读写的实现

openfileoutput和openfileinput方法

 /**
   * openfileoutput ,openfileinput
   * 这两种方法同sp一样只能讲文件保存到手机内存固定的路径中,
   * 默认为 /data/data/<packagename>/files
   */
  private void save2file() {
    try {
      //向文件写入内容
      fileoutputstream os = openfileoutput("file.txt", context.mode_private);
      string text = "写数据到文件";
      os.write(text.getbytes("utf-8"));

      //关闭流
      os.close();
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (unsupportedencodingexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }

  /**
   *
   */
  private void readfile() {

    try {
      fileinputstream ins = openfileinput("file.txt");
      byte[] buffer = new byte[100];
      int bytecount = ins.read(buffer);
      string text = new string(buffer,0,bytecount,"utf-8");
      toast.maketext(this,text,toast.length_short).show();

      ins.close();

    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }

文件存储位置

/data/data/<package-name>/files目录下

openfileoutput和openfileinput方法可以获得操作文件的outputstream以及inputstream对象,而且可以通过流对象处理任何文件的数据,但是这两个方法同sharedpreferences一样,只能在手机内存卡的指定目录建立文件,因此在使用上仍然有一定的局限性。

读取sd卡上的文件android数据存储之文件存储方法
main_activity.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/linearlayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.jay.example.filedemo2.mainactivity">

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="清输入文件名" />

  <edittext
    android:id="@+id/edittitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="文件名" />


  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="清输入文件内容" />

  <edittext
    android:id="@+id/editdetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="文件内容" />

  <button
    android:id="@+id/btnsave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="保存到sd卡" />

  <button
    android:id="@+id/btnclean"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="清空" />

  <button
    android:id="@+id/btnread"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="读取sd卡中的文件" />

</linearlayout>

接着我们来写一个sd操作类: sdfilehelper.java

public class sdfilehelper {

  private context context;

  public sdfilehelper() {
  }

  public sdfilehelper(context context) {
    super();
    this.context = context;
  }

  //往sd卡写入文件的方法
  public void savafiletosd(string filename, string filecontent) throws exception {
    //如果手机已插入sd卡,且app具有读写sd卡的权限
    if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
      filename = environment.getexternalstoragedirectory().getcanonicalpath() + "/" + filename;
      //这里就不要用openfileoutput了,那个是往手机内存中写数据的
      fileoutputstream output = new fileoutputstream(filename);
      output.write(filecontent.getbytes());
      //将string字符串以字节流的形式写入到输出流中
      output.close();
      //关闭输出流
    } else toast.maketext(context, "sd卡不存在或者不可读写", toast.length_short).show();
  }

  //读取sd卡中文件的方法
  //定义读取文件的方法:
  public string readfromsd(string filename) throws ioexception {
    stringbuilder sb = new stringbuilder("");
    if (environment.getexternalstoragestate().equals(environment.media_mounted)) {
      filename = environment.getexternalstoragedirectory().getcanonicalpath() + "/" + filename;
      //打开文件输入流
      fileinputstream input = new fileinputstream(filename);
      byte[] temp = new byte[1024];

      int len = 0;
      //读取文件内容:
      while ((len = input.read(temp)) > 0) {
        sb.append(new string(temp, 0, len));
      }
      //关闭输入流
      input.close();
    }
    return sb.tostring();
  }


}

接着mainactivity.java实现相关逻辑:

public class mainactivity extends appcompatactivity implements view.onclicklistener{

  private edittext editname;
  private edittext editdetail;
  private button btnsave;
  private button btnclean;
  private button btnread;
  private context mcontext;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    mcontext = getapplicationcontext();
    bindviews();
  }

  private void bindviews() {
    editname = (edittext) findviewbyid(r.id.edittitle);
    editdetail = (edittext) findviewbyid(r.id.editdetail);
    btnsave = (button) findviewbyid(r.id.btnsave);
    btnclean = (button) findviewbyid(r.id.btnclean);
    btnread = (button) findviewbyid(r.id.btnread);

    btnsave.setonclicklistener(this);
    btnclean.setonclicklistener(this);
    btnread.setonclicklistener(this);
  }

  @override
  public void onclick(view v) {
    switch (v.getid()){
      case r.id.btnclean:
        editdetail.settext("");
        editname.settext("");
        break;
      case r.id.btnsave:
        string filename = editname.gettext().tostring();
        string filedetail = editdetail.gettext().tostring();
        sdfilehelper sdhelper = new sdfilehelper(mcontext);
        try
        {
          sdhelper.savafiletosd(filename, filedetail);
          toast.maketext(getapplicationcontext(), "数据写入成功", toast.length_short).show();
        }
        catch(exception e){
          e.printstacktrace();
          toast.maketext(getapplicationcontext(), "数据写入失败", toast.length_short).show();
        }
        break;
      case r.id.btnread:
        string detail = "";
        sdfilehelper sdhelper2 = new sdfilehelper(mcontext);
        try
        {
          string filename2 = editname.gettext().tostring();
          detail = sdhelper2.readfromsd(filename2);
        }
        catch(ioexception e){e.printstacktrace();}
        toast.maketext(getapplicationcontext(), detail, toast.length_short).show();
        break;
    }
  }
}

最后别忘记在androidmanifest.xml写上读写sd卡的权限哦!

<!-- 在sdcard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.mount_unmount_filesystems"/>
<!-- 往sdcard写入数据权限 -->
<uses-permission android:name="android.permission.write_external_storage"/>

如何判断虚拟和物理两种sdk

在默认情况下,会将一部分存储空间分给虚拟的sd卡使用(一部分用于安装android操作系统)

android.os.enviroment.isexternalstorageremovalbe()

返回true:sd卡是物理的,反之sd卡是虚拟的。

用于适配不同型号手机,反射获取sd卡路径和状态

package com.turing.base.activity.datastore.filestore;

import android.content.context;
import android.os.environment;
import android.os.statfs;
import android.os.storage.storagemanager;
import android.text.textutils;
import android.util.log;

import java.io.file;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.concurrent.concurrentlinkedqueue;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;

/**
 * 用于适配不同型号手机,反射获取sd卡路径和状态
 *
 */
public class devmountinfo {

  private final string tag = devmountinfo.class.getsimplename();

  private static final int error = -1;

  // class name
  private final static string class_name = "android.os.storage.storagevolume";

  //remained spare memory size
  private static final int remained_spare_in_mb = 100;

  // method name
  private final static string method_get_volume_list = "getvolumelist";
  private final static string method_get_volume_state = "getvolumestate";
  private final static string method_is_removable = "isremovable";
  private final static string method_get_path = "getpath";

  private final static string mounted = "mounted";

  private static devmountinfo instance;

  private string msdcardpath = null;

  // internal file path
  private concurrentlinkedqueue<string> minternalpathlist = new concurrentlinkedqueue<string>();
  // external file path
  private concurrentlinkedqueue<string> mexternalpathlist = new concurrentlinkedqueue<string>();

  private executorservice mexecutor = null;

  private devmountinfo() {
    mexecutor = executors.newsinglethreadexecutor();
  }

  public static devmountinfo getinstance() {
    synchronized (devmountinfo.class) {
      if (null == instance) {
        instance = new devmountinfo();
      }
      return instance;
    }
  }

  @override
  protected void finalize() throws throwable {
    super.finalize();
    synchronized (devmountinfo.class) {
      minternalpathlist.clear();
      mexternalpathlist.clear();
      mexecutor.shutdown();
      instance = null;
    }
  }

  public void init(final context context) {
    mexecutor.execute(new runnable() {
      @override
      public void run() {
        executeinit(context);
      }
    });
  }

  public boolean issdcardfull() {
    return remained_spare_in_mb > (getsdcardavailspace() * 1024);
  }

  public boolean issdcardavaiable() {

    return !mexternalpathlist.isempty() || !minternalpathlist.isempty();
  }

  public string getsdcardpath() {
    return msdcardpath;
  }

  public long getsdcardtotalspace() {
    long totalspace = 0;
    if (!textutils.isempty(msdcardpath)) {
      statfs sf = new statfs(msdcardpath);
      long blocksize = sf.getblocksize();
      long total = sf.getblockcount();
      totalspace = total * blocksize / 1024;
    }
    return totalspace;
  }

  public long getsdcardavailspace() {
    long availspace = 0;
    if (!textutils.isempty(msdcardpath)) {
      statfs sf = new statfs(msdcardpath);
      long blocksize = sf.getblocksize();
      long availcount = sf.getavailableblocks();
      availspace = availcount * blocksize / 1024;
    }
    return availspace;
  }

  public string getinternalsdcardpath() {

    return minternalpathlist.peek();
  }

  public string getexternalsdcardpath() {

    return mexternalpathlist.peek();
  }

  private void executeinit(context context) {
    storagemanager mstoragemanager = (storagemanager) context.getsystemservice(context.storage_service);
    if (mstoragemanager != null) {
      class<?> mstoragevolume = null;
      method mgetvolumelistmethod = null;
      method mgetvolumestatemethod = null;
      method mgetpathmethod = null;
      method misremovablemethod = null;
      object[] mstoragevolumelist = null;
      try {
        mstoragevolume = class.forname(class_name);
        mgetvolumelistmethod = mstoragemanager.getclass().getmethod(method_get_volume_list, new class[0]);
        mgetvolumestatemethod = mstoragemanager.getclass().getmethod(method_get_volume_state, new class[]{string.class});
        misremovablemethod = mstoragevolume.getmethod(method_is_removable, new class[0]);
        mgetpathmethod = mstoragevolume.getmethod(method_get_path, new class[0]);

        mstoragevolumelist = (object[]) mgetvolumelistmethod.invoke(mstoragemanager, new object[0]);

        boolean misremovable = false;

        if (mstoragevolumelist != null && mstoragevolumelist.length > 0) {
          int mstoragevolumecount = mstoragevolumelist.length;

          log.i(tag, "init() === > storagevolume count = " + mstoragevolumecount);


          minternalpathlist.clear();
          mexternalpathlist.clear();

          for (int i = 0; i < mstoragevolumecount; ++i) {
            string mstoragepath = (string) mgetpathmethod.invoke(mstoragevolumelist[i], new object[0]);
            misremovable = ((boolean) misremovablemethod.invoke(mstoragevolumelist[i], new object[0])).booleanvalue();
            if (!textutils.isempty(mstoragepath)) {
              string state = (string) mgetvolumestatemethod.invoke(mstoragemanager, new object[]{mstoragepath});
              if ((state != null) && (state.equals(mounted))) {
                if (misremovable) {
                  log.i(tag, "init() === > external storage path = (" + mstoragepath + ")");
                  mexternalpathlist.add(mstoragepath);
                } else {
                  log.i(tag, "init() === > internal storage path = (" + mstoragepath + ")");
                  minternalpathlist.add(mstoragepath);
                }
              }
            }
          }
        }
      } catch (classnotfoundexception e) {
        handleinvalid();
        log.e(tag, "init() === > exception:classnotfoundexception");
      } catch (nosuchmethodexception e) {
        handleinvalid();
        log.e(tag, "init() === > exception:nosuchmethodexception");
      } catch (illegalargumentexception e) {
        handleinvalid();
        log.e(tag, "init() === > exception:illegalargumentexception");
      } catch (illegalaccessexception e) {
        handleinvalid();
        log.e(tag, "init() === > exception:illegalaccessexception");
      } catch (invocationtargetexception e) {
        handleinvalid();
        log.e(tag, "init() === > exception:invocationtargetexception");
      }
    } else {
      handleinvalid();
      log.e(tag, "init() === > can't get storage manager");
    }
    initsdcardpath();
  }

  private void handleinvalid() {
    minternalpathlist.add(environment.getexternalstoragedirectory().getpath());
  }

  private void initsdcardpath() {
    if (!mexternalpathlist.isempty()) {
      msdcardpath = mexternalpathlist.peek();
    } else if (!minternalpathlist.isempty()) {
      msdcardpath = minternalpathlist.peek();
    } else {
      msdcardpath = environment.getexternalstoragedirectory().getpath();
    }
    log.i(tag, "initsdcardpath() === > sdcard path = (" + msdcardpath + ")");
  }


  /**
   * sdcard是否存
   */
  public static boolean externalmemoryavailable() {
    return android.os.environment.getexternalstoragestate().equals(
        android.os.environment.media_mounted);
  }

  /**
   * 获取手机内部剩余存储空间
   *
   * @return
   */
  public static long getavailableinternalmemorysize() {
    file path = environment.getdatadirectory();
    statfs stat = new statfs(path.getpath());
    long blocksize = stat.getblocksize();
    long availableblocks = stat.getavailableblocks();
    return availableblocks * blocksize;
  }

  /**
   * 获取手机内部总的存储空间
   *
   * @return
   */
  public static long gettotalinternalmemorysize() {
    file path = environment.getdatadirectory();
    statfs stat = new statfs(path.getpath());
    long blocksize = stat.getblocksize();
    long totalblocks = stat.getblockcount();
    return totalblocks * blocksize;
  }

  /**
   * 获取手机内置存储剩余存储空间
   *
   * @return
   */
  public static long getavailableinternalsystemmemorysize() {
    file path = environment.getrootdirectory();
    statfs stat = new statfs(path.getpath());
    long blocksize = stat.getblocksize();
    long availableblocks = stat.getavailableblocks();
    return availableblocks * blocksize;
  }

  /**
   * 获取手机内置存储总的存储空间
   *
   * @return
   */
  public static long gettotalinternalsystemmemorysize() {
    file path = environment.getrootdirectory();
    statfs stat = new statfs(path.getpath());
    long blocksize = stat.getblocksize();
    long totalblocks = stat.getblockcount();
    return totalblocks * blocksize;
  }

  /**
   * 获取sdcard剩余存储空间
   *
   * @return
   */
  public static long getavailableexternalmemorysize() {
    if (externalmemoryavailable()) {
      file path = environment.getexternalstoragedirectory();
      statfs stat = new statfs(path.getpath());
      long blocksize = stat.getblocksize();
      long availableblocks = stat.getavailableblocks();
      return availableblocks * blocksize;
    } else {
      return error;
    }
  }

  /**
   * 获取sdcard总的存储空间
   *
   * @return
   */
  public static long gettotalexternalmemorysize() {
    if (externalmemoryavailable()) {
      file path = environment.getexternalstoragedirectory();
      statfs stat = new statfs(path.getpath());
      long blocksize = stat.getblocksize();
      long totalblocks = stat.getblockcount();
      return totalblocks * blocksize;
    } else {
      return error;
    }
  }

  public static long getavailablememorysize(string path) {
    if (null == path)
      return 0;
    statfs stat = new statfs(path);
    long blocksize = stat.getblocksize();
    long availableblocks = stat.getavailableblocks();
    return availableblocks * blocksize;
  }


}

读取raw和assets文件夹下的文件

相信大家对两个文件夹并不陌生,如果我们不想自己的文件被编译成二进制文件的话, 我们可以把文件放到这两个目录下,而两者的区别如下:

  • res/raw:文件会被映射到r.java文件中,访问的时候直接通过资源id即可访问,而且 他不能有目录结构,就是不能再创建文件夹
  • assets:不会映射到r.java文件中,通过assetmanager来访问,能有目录结构,即, 可以自行创建文件夹。

读取文件资源:

res/raw:

inputstream is =getresources().openrawresource(r.raw.filename); 

assets:

assetmanager am = getassets(); 
inputstream is = am.open("filename");

sax引擎读取xml文件

sax引擎读取xml文件的原理:

sax技术在处理xml文件时并不一次性把xml文件装入内存,而是一边读一般解析。

使用sax处理xml需要一个handler对象,一般会使用org.xml.sax.helpers.defaulthandler的子类作为handler对象

因此,这就需要处理如下5个分析点,也可称为分析事件:

  1. 开始分析xml文件。该分析点表示sax引擎刚开始处理xml文件,还没有读取xml文件中的内容。该分析点对应于defaulthandler类中的startdocument()事件方法,可以在该方法中做一下初始化的工作!
  2. 开始处理每一个xml元素,也就是遇到<product>,<item>这样的起始标记,sax引擎每次扫描到新的xml元素的起始标记会触发这个分析事件,对应的事件分析方法是startelement,在该方法中可以获取当前元素的名称和元素属性的相关信息
  3. 处理完一个xml元素,也就是遇到</product>,</item>这样的结束标记,该分析点对应的事件方法是endelement,在该事件中可以获得当前处理完的元素的全部信息。
  4. 处理完xml文件。如果sax引擎将整个xml文件的内容都扫描完了,就到了这个分析点,该分析点对应的事件方法enddocument(),该事件方法可能不是必需的,如果最后有以下收尾工作,如释放一下资源,可以在该方法中完成!
  5. 读取字符分析点。这是最重要的分析点。如果没有这个分析点,前4步的处理相当于白跑一遍,虽然读取了xml文件中的所有内容,但并未保存这些内容,而这个分析点所对应的characters事件方法的主要作用就是保存sax引擎读取的xml文件中的内容。更准确地说是保存xml元素的文本,也就是<product>abc</product>中的abc。

code

res\raw\product.xml

<?xml version="1.0" encoding="utf-8"?>
<products>
  <product>
    <id>10</id>
    <name>电脑</name>
    <price>2067.25</price>
  </product>
  <product>
    <id>20</id>
    <name>微波炉</name>
    <price>520</price>
  </product>
  <product>
    <id>30</id>
    <name>洗衣机</name>
    <price>2400</price>
  </product>

</products>

product.java

public class product
{
  private int id;
  private string name;
  private float price;
  public int getid()
  {
    return id;
  }
  public void setid(int id)
  {
    this.id = id;
  }
  public string getname()
  {
    return name;
  }
  public void setname(string name)
  {
    this.name = name;
  }
  public float getprice()
  {
    return price;
  }
  public void setprice(float price)
  {
    this.price = price;
  }


}

xml2product.java(defaulthandler子类)

defaulthandler子类 ,核心类,负责处理分析点事件。

import org.xml.sax.attributes;
import org.xml.sax.saxexception;
import org.xml.sax.helpers.defaulthandler;

import java.util.arraylist;
import java.util.list;

public class xml2product extends defaulthandler {
  private list<product> products;
  private product product;


  private stringbuffer buffer = new stringbuffer();

  public list<product> getproducts() {
    return products;
  }

  @override
  public void characters(char[] ch, int start, int length)
      throws saxexception {

    buffer.append(ch, start, length);
    super.characters(ch, start, length);
  }

  @override
  public void startdocument() throws saxexception {
    // 开始分析xml文件,创建list对象用于保存分析完的product对象
    products = new arraylist<product>();

  }

  @override
  public void startelement(string uri, string localname, string qname,
               attributes attributes) throws saxexception {

    if (localname.equals("product")) {
      // 如果分析的是<product>标签,则创建一个product对象
      product = new product();
    }
    super.startelement(uri, localname, qname, attributes);
  }


  @override
  public void endelement(string uri, string localname, string qname)
      throws saxexception {
    if (localname.equals("product")) {
      // 处理完 <product>标签后 将product对象添加到products中
      products.add(product);
    } else if (localname.equals("id")) {
      // 设置id属性的值
      product.setid(integer.parseint(buffer.tostring().trim()));
      // 将标签内容的缓存区清空
      buffer.setlength(0);
    } else if (localname.equals("name")) {
      product.setname(buffer.tostring().trim());
      buffer.setlength(0);
    } else if (localname.equals("price")) {
      product.setprice(float.parsefloat(buffer.tostring().trim()));
      buffer.setlength(0);
    }
    super.endelement(uri, localname, qname);
  }
}

xml2javaobjectact

import android.app.alertdialog;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.util.xml;
import android.view.view;

import com.turing.base.r;

import java.io.inputstream;
import java.util.list;

public class xml2javaobjectact extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_xml2_java_object);
  }


  public void onclick_xmltoobject(view view) {
    try {
      // 打开资源文件
      inputstream is = getresources().openrawresource(r.raw.products);
      xml2product xml2product = new xml2product();
      // 开始分析priducts.xml文件
      android.util.xml.parse(is, xml.encoding.utf_8, xml2product);
      // 输出转换后的java对象
      list<product> products = xml2product.getproducts();
      string msg = "共" + products.size() + "个产品\n";
      for (product product : products) {
        msg += "id:" + product.getid() + " 产品名:" + product.getname()
            + " 价格:" + product.getprice() + "\n";
      }
      // 弹出对话框
      new alertdialog.builder(this).settitle("产品信息").setmessage(msg)
          .setpositivebutton("关闭", null).show();
    } catch (exception e) {

    }

  }
}

效果图
android数据存储之文件存储方法

code

activity_jar_zip.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onclick="onclick_jar_compress"
    android:text="用jar格式压缩文件" />

  <button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onclick="onclick_jar_uncompress"
    android:text="解压jar格式文件" />

  <button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onclick="onclick_zip_compress"
    android:text="用zip格式压缩文件" />

  <button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onclick="onclick_zip_uncompress"
    android:text="解压zip格式文件" />

</linearlayout>

jarzipact

import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.toast;

import com.turing.base.r;

import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.util.jar.jarentry;
import java.util.jar.jarinputstream;
import java.util.jar.jaroutputstream;
import java.util.zip.zipentry;
import java.util.zip.zipinputstream;
import java.util.zip.zipoutputstream;

public class jarzipact extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_jar_zip);
  }

  public void onclick_jar_compress(view view) {
    try {
      // 使用fileoutputstream对象指定一个要输出的压缩文件(file.jar)
      fileoutputstream fos = new fileoutputstream(
          android.os.environment.getexternalstoragedirectory()
              + "/file.jar");
      // 第一步 创建jaroutputstream对象
      jaroutputstream jos = new jaroutputstream(fos);
      // 第二步 创建一个jarentry对象,并指定待压缩文件在压缩包中的文件名
      jarentry jarentry = new jarentry("strings.xml");
      jos.putnextentry(jarentry);

      inputstream is = getresources().getassets().open("strings.xml");
      byte[] buffer = new byte[8192];
      int count = 0;
      // 第四步 写入数据
      while ((count = is.read(buffer)) >= 0) {
        jos.write(buffer, 0, count);
      }
      // 第五步 关闭当前的jarentry等对象
      is.close();
      jos.closeentry();
      jos.close();
      toast.maketext(this, "成功将strings.xml文件以jar格式压缩.", toast.length_long)
          .show();

    } catch (exception e) {
      toast.maketext(this, e.getmessage(), toast.length_long).show();
    }
  }

  public void onclick_jar_uncompress(view view) {
    try {
      // 定义要解压的文件
      string filename = android.os.environment
          .getexternalstoragedirectory() + "/file.jar";
      if (!new file(filename).exists()) {
        toast.maketext(this, "压缩文件不存在.", toast.length_long).show();
        return;
      }
      // 使用fileinputstream对象指定要解压的对象
      fileinputstream fis = new fileinputstream(filename);
      // 1 创建jarinputstream对象来读取压缩文件(file.jar)
      jarinputstream jis = new jarinputstream(fis);
      // 2 调用getnextjarentry方法打开压缩包中的第一个文件 ,如果有多个,多次调用该方法
      jarentry jarentry = jis.getnextjarentry();
      // 3 输出已解压的文件
      fileoutputstream fos = new fileoutputstream(
          android.os.environment.getexternalstoragedirectory()
              + "/" + jarentry.getname());

      byte[] buffer = new byte[8192];
      int count = 0;
      // 4 输出已解压的字节流
      while ((count = jis.read(buffer)) >= 0) {
        fos.write(buffer, 0, count);
      }
      // 5 关闭
      jis.closeentry();
      jis.close();
      fos.close();

      toast.maketext(this, "成功解压jar格式的文件.", toast.length_long).show();

    } catch (exception e) {
      toast.maketext(this, e.getmessage(), toast.length_long).show();
    }
  }

  public void onclick_zip_compress(view view) {
    try {
      // 指定了2个待压缩的w文件,都在assets目录中
      string[] filenames = new string[]
          {"main.xml", "strings.xml"};
      fileoutputstream fos = new fileoutputstream(
          android.os.environment.getexternalstoragedirectory()
              + "/file.zip");
      zipoutputstream zos = new zipoutputstream(fos);
      int i = 1;
      //枚举filenames中的所有待压缩文件
      while (i <= filenames.length) {
        // 从filenames数组中取出当前待压缩的温佳明,作为压缩后的文件名,以保持要说前后文件名称一致
        zipentry zipentry = new zipentry(filenames[i - 1]);
        // 打开当前的zipentry对象
        zos.putnextentry(zipentry);


        inputstream is = getresources().getassets().open(
            filenames[i - 1]);
        byte[] buffer = new byte[8192];
        int count = 0;
        // 写入数据
        while ((count = is.read(buffer)) >= 0) {
          zos.write(buffer, 0, count);
        }
        zos.flush();
        // 关闭当前的zipentry对象
        zos.closeentry();
        is.close();
        i++;

      }
      zos.finish();
      zos.close();
      toast.maketext(this, "成功将main.xml、strings.xml文件以zip格式压缩.",
          toast.length_long).show();

    } catch (exception e) {
      toast.maketext(this, e.getmessage(), toast.length_long).show();
    }

  }

  public void onclick_zip_uncompress(view view) {
    try {

      // 指定待解压的文件
      string filename = android.os.environment
          .getexternalstoragedirectory() + "/file.zip";
      if (!new file(filename).exists()) {
        toast.maketext(this, "压缩文件不存在.", toast.length_long).show();
        return;
      }
      fileinputstream fis = new fileinputstream(filename);
      zipinputstream zis = new zipinputstream(fis);
      zipentry zipentry = null;
      // 通过不断调用getnextentry方法来解压file.zip中所有的文件
      while ((zipentry = zis.getnextentry()) != null) {
        fileoutputstream fos = new fileoutputstream(
            android.os.environment.getexternalstoragedirectory()
                + "/" + zipentry.getname());

        byte[] buffer = new byte[8192];
        int count = 0;
        while ((count = zis.read(buffer)) >= 0) {
          fos.write(buffer, 0, count);
        }
        zis.closeentry();
        fos.close();
      }
      zis.close();

      toast.maketext(this, "成功解压jar格式的文件.", toast.length_long).show();

    } catch (exception e) {
      toast.maketext(this, e.getmessage(), toast.length_long).show();
    }
  }
}

原文链接:http://blog.csdn.net/yangshangwei/article/details/50831269

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。