android 数据的解压与压缩

package com.example.zipdemo;



import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.zip.ZipOutputStream;


import org.apache.tools.zip.ZipEntry;


import android.os.Environment;

import android.util.Log;


/**

* Android Zip压缩解压缩

* @author ronald ([url]www.r-base.net[/url])

*/

public final class ZipUitl {

private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte


  private ZipUitl(){

  }


  /**

   * 取得压缩包中的 文件列表(文件夹,文件自选)

   * @param zipFileString                压缩包名字

   * @param bContainFolder        是否包括 文件夹

   * @param bContainFile                是否包括 文件

   * @return

   * @throws Exception

   */

  public static java.util.List<java.io.File> getFileList(String zipFileString,boolean bContainFolder, 

          boolean bContainFile)throws Exception {

    java.util.List<java.io.File> fileList = new java.util.ArrayList<java.io.File>();

    java.util.zip.ZipInputStream inZip = 

                     new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));

    java.util.zip.ZipEntry zipEntry;

    String szName = "";                

    while ((zipEntry = inZip.getNextEntry()) !=null) {

        szName = zipEntry.getName();

        if (zipEntry.isDirectory()) {

          // get the folder name of the widget

          szName = szName.substring(0, szName.length() - 1);

          java.io.File folder = new java.io.File(szName);

          if (bContainFolder) {

            fileList.add(folder);

          }

        } else {

          java.io.File file = new java.io.File(szName);

          if (bContainFile) {

            fileList.add(file);

          }

        }

    }//end of while                

    inZip.close();

    return fileList;

  }


  /**

   * 返回压缩包中的文件InputStream

   * 

   * @param zipFilePath                压缩文件的名字

   * @param fileString        解压文件的名字

   * @return InputStream

   * @throws Exception

   */

  public static java.io.InputStream upZip(String zipFilePath, String fileString)throws Exception {

        java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(zipFilePath);

        java.util.zip.ZipEntry zipEntry = zipFile.getEntry(fileString);


        return zipFile.getInputStream(zipEntry);

  }


/**

* 解压一个压缩文档 到指定位置

* @param zipFileString        压缩包的名字

* @param outPathString        指定的路径

* @throws Exception

*/

  public staticvoid unZipFolder(InputStream input, String outPathString)throws Exception {

        java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input);

        java.util.zip.ZipEntry zipEntry = null;

        String szName = "";


        while ((zipEntry = inZip.getNextEntry()) !=null) {

                szName = zipEntry.getName();


                if (zipEntry.isDirectory()) {

                  // get the folder name of the widget

                  szName = szName.substring(0, szName.length() - 1);

                  java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);

                  folder.mkdirs();

                } else {

                  java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);

                  file.createNewFile();

                  // get the output stream of the file

                  java.io.FileOutputStream out = new java.io.FileOutputStream(file);

                  int len;

                  byte[] buffer = new byte[1024];

                  // read (len) bytes into buffer

                  while ((len = inZip.read(buffer)) != -1) {

                        // write (len) byte from buffer at the position 0

                        out.write(buffer, 0, len);

                        out.flush();

                  }

                  out.close();

                }

        }//end of while

                inZip.close();

        }


        /**

         * 解压一个压缩文档 到指定位置

         * @param zipFileString        压缩包的名字

         * @param outPathString        指定的路径

         * @throws Exception

         */

        public staticvoid unZipFolder(String zipFileString, String outPathString)throws Exception {

                unZipFolder(new java.io.FileInputStream(zipFileString),outPathString);

        }//end of func



        /**

         * 压缩文件,文件夹

         * 

         * @param srcFilePath        要压缩的文件/文件夹名字

         * @param zipFilePath        指定压缩的目的和名字

         * @throws Exception

         */

        public staticvoid zipFolder(String srcFilePath, String zipFilePath)throws Exception {

          //创建Zip包

        File file1 =new File("/storage/emulated/0/DCIM/Camera/zi.zip");

        Log.i("fangyf"," file path + " + file1.getPath());

        if(file1.exists()){

        Log.i("fangyf","file exist");

        }else{

        Log.i("fangyf","file not exist");

        file1.createNewFile();

        }

          java.util.zip.ZipOutputStream outZip = 

              new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(file1));


          //打开要输出的文件

          java.io.File file = new java.io.File(srcFilePath);


          //压缩

          zipFiles(file.getParent()+java.io.File.separator, file.getName(), outZip);


          //完成,关闭

          outZip.finish();

          outZip.close();


        }//end of func


       

http://download.csdn.net/detail/woyulove/6605609
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值