背景
把文件压里头
把文件夹压里头
相关参考
我之前的一个文章,文件递归复制思路link
思路
如果把单单一个文件压缩,那么恭喜你,不用看这个文章了,因为百度一搜就有不错的答案。
本文主要介绍,如果把整个文件夹压缩成一个文件,如何把一个压缩文件,解压出来
开发
因为是安卓原生api,这里就不作环境介绍了。
文末有全部代码!!!
文件夹压缩
首先,外部调用这个方法,就是传入一个需要压缩的文件夹的路径即可。例如/ddd/ddd/
这种,那么,代码就会进行一个判断:
(1)如果是目录,则创建目录
(2)如果是文件,那就搞里头
涉及的操作对象:ZipEntry,ZipOutputStream
注意的点:
(1)源头目录的层级,和压缩文件目录的层级,是不一样的!!!
(2)如果是目录,同样的,也需要在压缩文件中创建,保持源头的目录层级,和压缩文件的目录层级一致!!!
关键api:
zipOutputSteam.putNextEntry(zipEntry);
这个entry,就是你放入压缩文件中的操作对象,可以是文件,也可以是目录!!
核心代码如下:
try {File file = new File(folderString + fileString);if (file.isFile()) {ZipEntry zipEntry = new ZipEntry(fileString);inputStream = new FileInputStream(file);zipOutputSteam.putNextEntry(zipEntry);int len;byte[] buffer = new byte[4096];while ((len = inputStream.read(buffer)) != -1) {zipOutputSteam.write(buffer, 0, len);}zipOutputSteam.closeEntry();} else {//文件夹//无论有没有子文件,都进行文件夹的创建ZipEntry zipEntry = new ZipEntry(fileString + File.separator);zipOutputSteam.putNextEntry(zipEntry);zipOutputSteam.closeEntry();String[] childFileList = file.list();//没有子文件和压缩//子文件和递归if (childFileList != null) {for (int i = 0; i < childFileList.length; i++) {File cacheFile = new File(childFileList[i]);String folderResult = folderString;ZipFiles(folderResult, fileString + File.separator + cacheFile.getName(), zipOutputSteam);}}}}
其实递归的逻辑,和文件夹递归复制,是一样的,区别就是,压缩文件将会多一个ZipEntry操作的流程。
解压
对于解压,这里同样,也是判断是目录,还是文件,
若是目录,则创建,若是文件,则通过ZipFile.getInputStream后,进行文件流本地生成。
核心代码如下:
private static void unZipChild(ZipFile zipFile, ZipEntry zipEntry, String targetFolder) {try {if (zipEntry.isDirectory()) {String targetDir = targetFolder + zipEntry.getName();new File(targetDir).mkdirs();} else {InputStream inputStream = zipFile.getInputStream(zipEntry);FileOutputStream outputStream = new FileOutputStream(new File(targetFolder, zipEntry.getName()));byte[] buffer = new byte[1024];int length;while ((length = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, length);}outputStream.close();inputStream.close();}} catch (Exception e) {e.printStackTrace();}}
上面是文件夹压缩和解压的全部思路,下面放出所有的相关代码
public class ZipUtils {/*** 压缩文件** @param folderString* @param fileString* @param zipOutputSteam* @throws Exception*/private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception {if (zipOutputSteam == null)return;FileInputStream inputStream = null;try {File file = new File(folderString + fileString);if (file.isFile()) {ZipEntry zipEntry = new ZipEntry(fileString);inputStream = new FileInputStream(file);zipOutputSteam.putNextEntry(zipEntry);int len;byte[] buffer = new byte[4096];while ((len = inputStream.read(buffer)) != -1) {zipOutputSteam.write(buffer, 0, len);}zipOutputSteam.closeEntry();} else {//文件夹//无论有没有子文件,都进行文件夹的创建ZipEntry zipEntry = new ZipEntry(fileString + File.separator);zipOutputSteam.putNextEntry(zipEntry);zipOutputSteam.closeEntry();String[] childFileList = file.list();//没有子文件和压缩//子文件和递归if (childFileList != null) {for (int i = 0; i < childFileList.length; i++) {File cacheFile = new File(childFileList[i]);String folderResult = folderString;ZipFiles(folderResult, fileString + File.separator + cacheFile.getName(), zipOutputSteam);}}}} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException ex) {ex.printStackTrace();}}}}// 递归删除目录及其子目录和文件public static boolean deleteDirectory(File dir) {if (dir.isDirectory()) {String[] children = dir.list();for (String child : children) {boolean success = deleteDirectory(new File(dir, child));if (!success) {return false;}}}return dir.delete();}private static void unZipChild(ZipFile zipFile, ZipEntry zipEntry, String targetFolder) {try {if (zipEntry.isDirectory()) {String targetDir = targetFolder + zipEntry.getName();new File(targetDir).mkdirs();} else {InputStream inputStream = zipFile.getInputStream(zipEntry);FileOutputStream outputStream = new FileOutputStream(new File(targetFolder, zipEntry.getName()));byte[] buffer = new byte[1024];int length;while ((length = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, length);}outputStream.close();inputStream.close();}} catch (Exception e) {e.printStackTrace();}}//解压------------------------------------------------------------------------------------/*** @param zipFileName 压缩文件路径* @param targetFolder 解压出来的路径*/public static void unZip(String zipFileName, String targetFolder) {try {deleteDirectory(new File(targetFolder));File targetFile = new File(targetFolder);if (!targetFile.exists()) {targetFile.mkdirs();}ZipFile zipFile = new ZipFile(zipFileName);Enumeration<? extends ZipEntry> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry zipEntry = entries.nextElement();// 在这里可以对每个条目进行操作unZipChild(zipFile, zipEntry, targetFolder);}zipFile.close();} catch (IOException e) {e.printStackTrace();}}//压缩------------------------------------------------------------------------------------/*** 压缩文件和文件夹** @param srcFileString 要压缩的文件或文件夹* @param zipFileString 压缩完成的Zip路径* @throws Exception*/public static void ZipFolder(String srcFileString, String zipFileString, String zipName) {//创建ZIPZipOutputStream outZip = null;try {//创建文件File file = new File(srcFileString);File outPath = new File(zipFileString);if (!outPath.exists()) {outPath.mkdirs();}outZip = new ZipOutputStream(new FileOutputStream(new File(outPath, zipName)));//压缩ZipFiles(file.getParent() + File.separator, file.getName(), outZip);} catch (Exception e) {e.printStackTrace();} finally {//完成和关闭if (outZip != null) {try {outZip.flush();outZip.finish();outZip.close();} catch (IOException e) {e.printStackTrace();}}}}}
that’s all------------------------------------------------------------------------------------------------------