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

本文实例讲述了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程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, zip
, 文件压缩
解压缩
java实现解压缩zip、android 解压缩zip、zip解压缩软件、7zip解压缩软件安卓、zip解压缩文件名乱码,以便于您获取更多的相关知识。

时间: 2024-08-03 18:55:06

Android实现zip文件压缩及解压缩的方法_Android的相关文章

Android实现把文件存放在SDCard的方法_Android

本文实例讲述了Android实现把文件存放在SDCard的方法.分享给大家供大家参考.具体如下: 使用Activity的openFileOutput()方法保存文件,文件是存放在手机空间上,一般手机的存储空间不是很大,存放些小文件还行,如果要存放像视频这样的大文件,是不可行的.对于像视频这样的大文件,我们可以把它存放在SDCard. SDCard是干什么的?你可以把它看作是移动硬盘或U盘. 在模拟器中使用SDCard,你需要先创建一张SDCard卡(当然不是真的SDCard,只是镜像文件).创建

Java文件压缩与解压缩(一)

package com.cn; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutpu

commons-compress操作zip文件(压缩和解压缩)

commons-compress操作zip文件(压缩和解压缩) 有需要的同学可参考一下. Apache Commons Compress是一个压缩.解压缩文件的类库. 可以操作ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大. 在这里写两个用Commons Compress把文件压缩成zip和从zip解压缩的方法. 直接贴上工具类代码.    代码如下 复制代码 package cn.luxh.utils

asp.net c# ZIP文件压缩与解压

asp教程.net c# zip文件压缩与解压 using system; using system.collections.generic; using system.text; using system.io; using icsharpcode.sharpziplib.zip; using system.diagnostics; using icsharpcode.sharpziplib.core; namespace testconsole {     class program    

Android实现把文件存放在SDCard的方法

本文实例讲述了Android实现把文件存放在SDCard的方法.分享给大家供大家参考.具体如下: 使用Activity的openFileOutput()方法保存文件,文件是存放在手机空间上,一般手机的存储空间不是很大,存放些小文件还行,如果要存放像视频这样的大文件,是不可行的.对于像视频这样的大文件,我们可以把它存放在SDCard. SDCard是干什么的?你可以把它看作是移动硬盘或U盘. 在模拟器中使用SDCard,你需要先创建一张SDCard卡(当然不是真的SDCard,只是镜像文件).创建

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>

iOS开发 - 文件压缩与解压缩

第三方解压缩框架--SSZipArchive 下载地址:https://github.com/samsoffes/ssziparchive  注意:需要引入libz.dylib框架  <code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: borde

python zip文件 压缩_python

从简单的角度来看的话,zip格式会是个不错的选择,而且python对zip格式的支持够简单,够好用.1)简单应用 如果你仅仅是希望用python来做压缩和解压缩,那么就不用去翻文档了,这里提供一个简单的用法,让你一看就能明白. import zipfile f = zipfile.ZipFile('filename.zip', 'w' ,zipfile.ZIP_DEFLATED) f.write('file1.txt') f.write('file2.doc') f.write('file3.r

Java文件压缩与解压缩(二)

package com.cn; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; i