文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-06 09:56

关注

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

DirTraversal.java如下:


package com.once;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;

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;

public class ZipUtils {
  private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
  
  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();
  }
  
  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();
  }
  
  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();
    }
  }
  
  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;
  }
  
  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;
  }
  
  public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException,
      IOException {
    ZipFile zf = new ZipFile(zipFile);
    return zf.entries();
  }
  
  public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
    return new String(entry.getComment().getBytes("GB2312"), "8859_1");
  }
  
  public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
    return new String(entry.getName().getBytes("GB2312"), "8859_1");
  }
  
  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中文件的压缩和解压缩实例代码Android实现下载zip压缩文件并解压的方法(附源码)Android如何实现压缩和解压缩文件Android编程实现将压缩数据库文件拷贝到安装目录的方法android打开rar压缩文件Android实现文件或文件夹压缩成.zip格式压缩包


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯