Android--文件夹压缩为.zip

简介: 版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/64439638 package com.
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/64439638
package com.zipUtil;
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;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
 
 public static void zip(String src, String dest) throws IOException {
  //提供了一个数据项压缩成一个ZIP归档输出流
  ZipOutputStream out = null;
  try {
   
   File outFile = new File(dest);//源文件或者目录
   File fileOrDirectory = new File(src);//压缩文件路径
   out = new ZipOutputStream(new FileOutputStream(outFile));
   //如果此文件是一个文件,否则为false。
   if (fileOrDirectory.isFile()) {
    zipFileOrDirectory(out, fileOrDirectory, "");
   } else {
    //返回一个文件或空阵列。
    File[] entries = fileOrDirectory.listFiles();
    for (int i = 0; i < entries.length; i++) {
     // 递归压缩,更新curPaths
     zipFileOrDirectory(out, entries[i], "");
    }
   }
  } catch (IOException ex) {
   ex.printStackTrace();
  } finally {
   //关闭输出流
   if (out != null) {
    try {
     out.close();
    } catch (IOException ex) {
     ex.printStackTrace();
    }
   }
  }
 }
 
 private static void zipFileOrDirectory(ZipOutputStream out,
   File fileOrDirectory, String curPath) throws IOException {
  //从文件中读取字节的输入流
  FileInputStream in = null;
  try {
   //如果此文件是一个目录,否则返回false。
   if (!fileOrDirectory.isDirectory()) {
    // 压缩文件
    byte[] buffer = new byte[4096];
    int bytes_read;
    in = new FileInputStream(fileOrDirectory);
    //实例代表一个条目内的ZIP归档
    ZipEntry entry = new ZipEntry(curPath
      + fileOrDirectory.getName());
    //条目的信息写入底层流
    out.putNextEntry(entry);
    while ((bytes_read = in.read(buffer)) != -1) {
     out.write(buffer, 0, bytes_read);
    }
    out.closeEntry();
   } else {
    // 压缩目录
    File[] entries = fileOrDirectory.listFiles();
    for (int i = 0; i < entries.length; i++) {
     // 递归压缩,更新curPaths
     zipFileOrDirectory(out, entries[i], curPath
       + fileOrDirectory.getName() + "/");
    }
   }
  } catch (IOException ex) {
   ex.printStackTrace();
   // throw ex;
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException ex) {
     ex.printStackTrace();
    }
   }
  }
 }
 
 @SuppressWarnings("unchecked")
 public static void unzip(String zipFileName, String outputDirectory)
   throws IOException {
  ZipFile zipFile = null;
  try {
   zipFile = new ZipFile(zipFileName);
   Enumeration e = zipFile.entries();
   ZipEntry zipEntry = null;
   File dest = new File(outputDirectory);
   dest.mkdirs();
   while (e.hasMoreElements()) {
    zipEntry = (ZipEntry) e.nextElement();
    String entryName = zipEntry.getName();
    InputStream in = null;
    FileOutputStream out = null;
    try {
     if (zipEntry.isDirectory()) {
      String name = zipEntry.getName();
      name = name.substring(0, name.length() - 1);
      File f = new File(outputDirectory + File.separator
        + name);
      f.mkdirs();
     } else {
      int index = entryName.lastIndexOf("\\");
      if (index != -1) {
       File df = new File(outputDirectory + File.separator
         + entryName.substring(0, index));
       df.mkdirs();
      }
      index = entryName.lastIndexOf("/");
      if (index != -1) {
       File df = new File(outputDirectory + File.separator
         + entryName.substring(0, index));
       df.mkdirs();
      }
      File f = new File(outputDirectory + File.separator
        + zipEntry.getName());
      // f.createNewFile();
      in = zipFile.getInputStream(zipEntry);
      out = new FileOutputStream(f);
      int c;
      byte[] by = new byte[1024];
      while ((c = in.read(by)) != -1) {
       out.write(by, 0, c);
      }
      out.flush();
     }
    } catch (IOException ex) {
     ex.printStackTrace();
     throw new IOException("解压失败:" + ex.toString());
    } finally {
     if (in != null) {
      try {
       in.close();
      } catch (IOException ex) {
      }
     }
     if (out != null) {
      try {
       out.close();
      } catch (IOException ex) {
      }
     }
    }
   }
  } catch (IOException ex) {
   ex.printStackTrace();
   throw new IOException("解压失败:" + ex.toString());
  } finally {
   if (zipFile != null) {
    try {
     zipFile.close();
    } catch (IOException ex) {
    }
   }
  }
 }
}


调用:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Thread th  = new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					ZipUtil.zip(Environment.getExternalStorageDirectory().getPath()+"/baidu", Environment.getExternalStorageDirectory().getPath()+"/baidu.zip");
					ZipUtil.unzip(Environment.getExternalStorageDirectory().getPath()+"/baidu.zip", Environment.getExternalStorageDirectory().getPath());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});th.start();		
	}


目录
相关文章
|
8月前
|
XML Android开发 数据格式
Android drawable 与 mipmap 文件夹存放图片有区别
Android drawable 与 mipmap 文件夹存放图片有区别
60 0
|
11月前
|
Android开发
【Magisk模块】禁用Android 11-12应用文件夹限制
【Magisk模块】禁用Android 11-12应用文件夹限制
326 0
|
12月前
|
Java API Android开发
Android C++系列:访问Assets 文件夹.md
assets目录是Android的一种特殊目录,用于放置APP所需的固定文件,且该文件被打包到APK中时,不会被编码到二进制文件。 Android还存在一种放置在res下的raw目录,该目录与assets目录不同。
273 0
|
XML 存储 网络协议
【Android】使用Android开发应用过程中遇到ViewGroup的简单效以及aw和assets文件夹下的文件(Http协议的底层工作)
【Android】使用Android开发应用过程中遇到ViewGroup的简单效以及aw和assets文件夹下的文件(Http协议的底层工作)
130 0
【Android】使用Android开发应用过程中遇到ViewGroup的简单效以及aw和assets文件夹下的文件(Http协议的底层工作)
|
缓存 数据库 Android开发
新建文件夹,复制文件,glide保存文件android 获取sdcard,获取data.data 目录file.mkdirs() file.mkdir()Dev
新建文件夹,复制文件,glide保存文件android 获取sdcard,获取data.data 目录file.mkdirs() file.mkdir()Dev
137 0
|
Android开发
如何在Android Studio和eclipse中查看File Explorer视图(设备中的文件夹视图)
如何在Android Studio和eclipse中查看File Explorer视图(设备中的文件夹视图)
685 0
如何在Android Studio和eclipse中查看File Explorer视图(设备中的文件夹视图)
|
开发工具 Android开发
Android中Bitmap压缩的几种方法的解读
很久之前研究微信的分享sdk的时候,在缩略图这遇到了一点问题,就做了个笔记,最近才翻出来。 微信分享的缩略图要求是不大于32k,这就需要对图片进行压缩。试了几种方法,一一道来。
277 0
|
缓存 数据库 Android开发
【Android 逆向】APK 文件格式 ( Android 应用安装 | Zip 文件格式 | 使用 Python 代码提取 APK 文件 )
【Android 逆向】APK 文件格式 ( Android 应用安装 | Zip 文件格式 | 使用 Python 代码提取 APK 文件 )
540 1
【Android 逆向】APK 文件格式 ( Android 应用安装 | Zip 文件格式 | 使用 Python 代码提取 APK 文件 )
|
Android开发
【错误记录】Android Studio 编译报错 ( Gradle 下载错误导致 Failed to open zip file 报错 )
【错误记录】Android Studio 编译报错 ( Gradle 下载错误导致 Failed to open zip file 报错 )
180 0
【错误记录】Android Studio 编译报错 ( Gradle 下载错误导致 Failed to open zip file 报错 )
|
XML 安全 Android开发
【Android 安装包优化】开启资源压缩 ( 资源压缩配置 | 启用严格模式的资源引用检查 | 自定义保留/移除资源配置 | 资源压缩效果 )
【Android 安装包优化】开启资源压缩 ( 资源压缩配置 | 启用严格模式的资源引用检查 | 自定义保留/移除资源配置 | 资源压缩效果 )
253 0
【Android 安装包优化】开启资源压缩 ( 资源压缩配置 | 启用严格模式的资源引用检查 | 自定义保留/移除资源配置 | 资源压缩效果 )