Android压缩文件(压缩目录)

原文地址点击打开链接

在Android中我们很多时候需要进行压缩与解压缩,就如本人的[ 足球即时比分 ]应用中也用到过.需要将一些信息进行收集再进行压缩,最后将压缩文件上传到服务器中(如何上传将文件上传到服务器中可以看我另一篇帖子 :[ Android上传文件到服务器 ]).


  以下我的使用到的工具类的代码.需要注意的是,进行压缩与解压缩都不支持中文名,如果需要支持中文名的话,一般是使用 Ant中的ZipInputStream与ZipOutStream,由于手机上使用ant的jar包的话,会令应用或游戏的大小变大很多,所以尽量小引入其它第三方的jar包的.

package rbase.app.nowscore.util;

import java.io.InputStream;

/**
* Android Zip压缩解压缩
* @author ronald ([url]www.r-base.net[/url])
*/
public final class ZipUtil {
  private ZipUtil(){
  }

  /**
   * 取得压缩包中的 文件列表(文件夹,文件自选)
   * @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 static void 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 static void unZipFolder(String zipFileString, String outPathString)throws Exception {
                unZipFolder(new java.io.FileInputStream(zipFileString),outPathString);
        }//end of func


        /**
         * 压缩文件,文件夹
         * 
         * @param srcFilePath        要压缩的文件/文件夹名字
         * @param zipFilePath        指定压缩的目的和名字
         * @throws Exception
         */
        public static void zipFolder(String srcFilePath, String zipFilePath)throws Exception {
          //创建Zip包
          java.util.zip.ZipOutputStream outZip = 
              new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(zipFilePath));

          //打开要输出的文件
          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

        /**
         * 压缩文件
         * @param folderPath
         * @param filePath
         * @param zipOut
         * @throws Exception
         */
        private static void zipFiles(String folderPath, String filePath, 
                     java.util.zip.ZipOutputStream zipOut)throws Exception{
          if(zipOut == null){
            return;
          }

          java.io.File file = new java.io.File(folderPath+filePath);

          //判断是不是文件
          if (file.isFile()) {
            java.util.zip.ZipEntry zipEntry =  new java.util.zip.ZipEntry(filePath);
            java.io.FileInputStream inputStream = new java.io.FileInputStream(file);
            zipOut.putNextEntry(zipEntry);

            int len;
            byte[] buffer = new byte[4096];

            while((len=inputStream.read(buffer)) != -1) {
                 zipOut.write(buffer, 0, len);
            }

             zipOut.closeEntry();
          } else {
           //文件夹的方式,获取文件夹下的子文件
           String fileList[] = file.list();

           //如果没有子文件, 则添加进去即可
           if (fileList.length <= 0) {
                  java.util.zip.ZipEntry zipEntry =  
                       new java.util.zip.ZipEntry(filePath+java.io.File.separator);
                zipOut.putNextEntry(zipEntry);
                zipOut.closeEntry();                                
           }

           //如果有子文件, 遍历子文件
           for (int i = 0; i < fileList.length; i++) {
                zipFiles(folderPath, filePath+java.io.File.separator+fileList[i], zipOut);
           }//end of for

         }//end of if

     }//end of func
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发压缩文件是一种常见的操作,可以用于将多个文件文件夹打包成一个压缩文件,或者解压缩已有的压缩文件Android提供了一些类和方法来实现文件压缩和解压缩。 1. 压缩文件Android常用的压缩文件格式是ZIP格式,可以使用java.util.zip包的类来进行压缩操作。以下是一个简单的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtils { public static void zipFile(String sourceFilePath, String zipFilePath) { try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); File file = new File(sourceFilePath); FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } fis.close(); zos.closeEntry(); zos.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码的`zipFile`方法接收两个参数,`sourceFilePath`表示要压缩文件路径,`zipFilePath`表示生成的压缩文件路径。该方法会将指定的文件压缩成一个ZIP文件。 2. 解压缩文件: 同样使用java.util.zip包的类来进行解压缩操作。以下是一个简单的示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipUtils { public static void unzipFile(String zipFilePath, String destDirectory) { try { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File newFile = new File(destDirectory + File.separator + fileName); FileOutputStream fos = new FileOutputStream(newFile); byte[] buffer = new byte[1024]; int length; while ((length = zis.read(buffer)) > 0) { fos.write(buffer, 0, length); } fos.close(); zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码的`unzipFile`方法接收两个参数,`zipFilePath`表示要解压缩的ZIP文件路径,`destDirectory`表示解压缩后的目标文件夹路径。该方法会将指定的ZIP文件压缩到目标文件。 以上是Android压缩文件和解压缩文件的简单示例代码,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值