package com.text.ziptest;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Enumeration;

import java.util.zip.CRC32;

import java.util.zip.CheckedInputStream;

import java.util.zip.CheckedOutputStream;

import java.util.zip.Deflater;

import java.util.zip.ZipException;

import java.util.zip.ZipInputStream;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

import android.util.Log;

/**

* 需要引入一个jar包:ant.jar

*/

/**

 * 

 * [一句话功能简述]<BR>

 * [功能详细描述]

 * 

 * @author zhouxin

 * @version [Android MTVClient C01, 2011-3-4]

 */

public class ZipControl {

private static boolean isCreateSrcDir = false;// 是否创建源目录


// 在这里的话需要说明下。如果需要创建源目录的话。就在这里设为true否则为false;


private static String TAG = "ZipControl";

/**





* [对指定路径下文件的压缩处理]<BR>


* [功能详细描述]





* @param src


*            径地址


* @param archive


*            指定到压缩文件夹的路径


* @param comment


*            描述


* @throws FileNotFoundException


*             文件没有找到异常


* @throws IOException


*             IO输入异常


*/


public void writeByApacheZipOutputStream(String[] src, String archive,


String comment) throws FileNotFoundException, IOException {


Log.e(TAG, "writeByApacheZipOutputStream");


// ----压缩文件:


FileOutputStream fos = new FileOutputStream(archive);


// 使用指定校验和创建输出流


CheckedOutputStream csum = new CheckedOutputStream(fos, new CRC32());


ZipOutputStream zos = new ZipOutputStream(csum);


// 支持中文


// zos.setEncoding("GBK");

BufferedOutputStream out = new BufferedOutputStream(zos);


// 设置压缩包注释


zos.setComment(comment);


// 启用压缩


zos.setMethod(ZipOutputStream.DEFLATED);


// 压缩级别为最强压缩,但时间要花得多一点


zos.setLevel(Deflater.BEST_COMPRESSION);


// 如果为单个文件的压缩在这里修改


for (int i = 0; i < src.length; i++) {


Log.e(TAG, "src[" + i + "] is " + src[i]);


File srcFile = new File(src[i]);


if (!srcFile.exists()


|| (srcFile.isDirectory() && srcFile.list().length == 0)) {


Log.e(TAG, "!srcFile.exists()");


throw new FileNotFoundException(


"File must exist and ZIP file must have at least one entry.");


}


String strSrcString = src[i];


// 获取压缩源所在父目录


strSrcString = strSrcString.replaceAll("////", "/");


String prefixDir = null;


if (srcFile.isFile()) {


prefixDir = strSrcString.substring(0,


strSrcString.lastIndexOf("/") + 1);


} else {


prefixDir = (strSrcString.replaceAll("/$", "") + "/");


}


// 如果不是根目录


if (prefixDir.indexOf("/") != (prefixDir.length() - 1)


&& isCreateSrcDir) {


prefixDir = prefixDir.replaceAll("[^/]+/$", "");


}


// 开始压缩


writeRecursive(zos, out, srcFile, prefixDir);


}

out.close();


// 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用


Log.e(TAG, "Checksum: " + csum.getChecksum().getValue());


@SuppressWarnings("unused")


BufferedInputStream bi;


}

/**





* [* 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的


* java.util.zip.ZipFile 使用方式是一新的,只不过多了设置编码方式的 接口。





* 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile 来读取压缩文件。]<BR>





* @param archive


*            压缩包路径


* @param decompressDir


*            解压路径


* @throws IOException


* @throws FileNotFoundException


* @throws ZipException


*/

public static void readByApacheZipFile(String archive, String decompressDir)


throws IOException, FileNotFoundException, ZipException {


Log.e(TAG, "readByApacheZipFile");


BufferedInputStream bi;


ZipFile zf = new ZipFile(archive, "GBK");//支持中文

        Enumeration e = zf.getEntries();


while (e.hasMoreElements()) {


ZipEntry ze2 = (ZipEntry) e.nextElement();


String entryName = ze2.getName();


String path = decompressDir + "/" + entryName;


if (ze2.isDirectory()) {


Log.e(TAG, "正在创建解压目录 - " + entryName);


File decompressDirFile = new File(path);


if (!decompressDirFile.exists()) {


decompressDirFile.mkdirs();


}


} else {


Log.e(TAG, "正在创建解压文件 - " + entryName);


String fileDir = path.substring(0, path.lastIndexOf("/"));


File fileDirFile = new File(fileDir);


if (!fileDirFile.exists()) {


fileDirFile.mkdirs();


}


BufferedOutputStream bos = new BufferedOutputStream(


new FileOutputStream(decompressDir + "/" + entryName));


bi = new BufferedInputStream(zf.getInputStream(ze2));


byte[] readContent = new byte[1024];


int readCount = bi.read(readContent);


while (readCount != -1) {


bos.write(readContent, 0, readCount);


readCount = bi.read(readContent);


}


bos.close();


}


}


zf.close();


}

/**





* [使用 java api 中的 ZipInputStream 类解压文件,但如果压缩时采用了


* org.apache.tools.zip.ZipOutputStream时,而不是 java 类库中的


* java.util.zip.ZipOutputStream时,该方法不能使用,原因就是编码方 式不一致导致,运行时会抛如下异常:


* java.lang.IllegalArgumentException at


* java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290)





* 当然,如果压缩包使用的是java类库的java.util.zip.ZipOutputStream 压缩而成是不会有问题的,但它不支持中文 ]<BR>


* [功能详细描述]





* @param archive


*            压缩包路径


* @param decompressDir


*            解压路径


* @throws FileNotFoundException


* @throws IOException


*/


public static void readByZipInputStream(String archive, String decompressDir)


throws FileNotFoundException, IOException {


BufferedInputStream bi;


// ----解压文件(ZIP文件的解压缩实质上就是从输入流中读取数据):


Log.e(TAG, "开始读压缩文件");


FileInputStream fi = new FileInputStream(archive);


CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32());


ZipInputStream in2 = new ZipInputStream(csumi);


bi = new BufferedInputStream(in2);


java.util.zip.ZipEntry ze;// 压缩文件条目


// 遍历压缩包中的文件条目


while ((ze = in2.getNextEntry()) != null) {


String entryName = ze.getName();


if (ze.isDirectory()) {


Log.e(TAG, "正在创建解压目录 - " + entryName);


File decompressDirFile = new File(decompressDir + "/"


+ entryName);


if (!decompressDirFile.exists()) {


decompressDirFile.mkdirs();


}


} else {


Log.e(TAG, "正在创建解压文件 - " + entryName);


BufferedOutputStream bos = new BufferedOutputStream(


new FileOutputStream(


decompressDir


+ "/"


+ entryName.substring(


entryName.lastIndexOf("//"),


entryName.length()


- (entryName


.lastIndexOf("//") - 2))));


byte[] buffer = new byte[1024];


int readCount = bi.read(buffer);


while (readCount != -1) {


bos.write(buffer, 0, readCount);


readCount = bi.read(buffer);


}


bos.close();


}


}


bi.close();


Log.e(TAG, "Checksum: " + csumi.getChecksum().getValue());


}

/**





* [递归压缩





* 使用 org.apache.tools.zip.ZipOutputStream 类进行压缩,它的好处就是支持中文路径, 而Java类库中的


* java.util.zip.ZipOutputStream 压缩中文文件名时压缩包会出现乱码。 使用 apache 中的这个类与 java


* 类库中的用法是一新的,只是能设置编码方式了。]<BR>


* [功能详细描述]





* @param zos


* @param bo


* @param srcFile


* @param prefixDir


* @throws IOException


* @throws FileNotFoundException


*/


private static void writeRecursive(ZipOutputStream zos,


BufferedOutputStream bo, File srcFile, String prefixDir)


throws IOException, FileNotFoundException {


Log.e(TAG, "writeRecursive");


ZipEntry zipEntry;


String filePath = srcFile.getAbsolutePath().replaceAll("////", "/")


.replaceAll("//", "/");


if (srcFile.isDirectory()) {


filePath = filePath.replaceAll("/$", "") + "/";


}


String entryName = filePath.replace(prefixDir, "").replaceAll("/$", "");


if (srcFile.isDirectory()) {


if (!"".equals(entryName)) {


Log.e(TAG, "正在创建目录 - " + srcFile.getAbsolutePath()


+ " entryName=" + entryName);


// 如果是目录,则需要在写目录后面加上 /


zipEntry = new ZipEntry(entryName + "/");


zos.putNextEntry(zipEntry);


}


File srcFiles[] = srcFile.listFiles();


for (int i = 0; i < srcFiles.length; i++) {


writeRecursive(zos, bo, srcFiles[i], prefixDir);


}


} else {


Log.e(TAG, "正在写文件 - " + srcFile.getAbsolutePath() + " entryName="


+ entryName);


BufferedInputStream bi = new BufferedInputStream(


new FileInputStream(srcFile));


// 开始写入新的ZIP文件条目并将流定位到条目数据的开始处


zipEntry = new ZipEntry(entryName);


zos.putNextEntry(zipEntry);


byte[] buffer = new byte[1024];


int readCount = bi.read(buffer);


while (readCount != -1) {


bo.write(buffer, 0, readCount);


readCount = bi.read(buffer);


}


// 注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不


// 然可能有的内容就会存入到后面条目中去了


bo.flush();


// 文件读完后关闭


bi.close();


}


}

}

可解压带中文名称文件的zip包的更多相关文章

  1. 非Windows系统 如何解压带中文密码和中文文件名的zip压缩文件

    数据科学交流群,群号:189158789 ,欢迎各位对数据科学感兴趣的小伙伴的加入! 一.安装unar软件包: Linux(Debian系列): apt install unarLinux(RedHa ...

  2. 解决ubuntu中zip解压的中文乱码问题

    转自解决ubuntu中zip解压的中文乱码问题 在我的ubuntu12.10中,发现显示中文基本都是正常的,只有在解压windows传过来的zip文件时,才会出现乱码.所以,我用另一个方法解决中文乱码 ...

  3. tar 解压某个指定的文件或者文件夹

    1. 先查看压缩文档中有那些文件,如果都不清楚文件内容,然后就直接解压,这个是不可能的 使用#tar -tf 压缩包名称,可以查看压缩包内容 2.解压某个文件 tar -zxvf zabbix.tar ...

  4. tar解压某个目录 tar解压某个指定的文件或者文件夹

    tar解压某个目录 tar解压某个指定的文件或者文件夹 发布时间:2017-05-30 来源:服务器之家   1. 先查看压缩文档中有那些文件,如果都不清楚文件内容,然后就直接解压,这个是不可能的 使 ...

  5. Linux部署Web应用程序超链接下载中文名称文件404问题解决办法

    Web应用程序目录下有帮助文档,是中文名称的Word文件 超链接内容如下: <a href="jsp/plugin/用户手册.doc">用户手册</a> 开 ...

  6. 如何解压POSIX tar archive文件

    下载了一个xxx.gz的文件,使用x xxx.gz(zsh的x插件,十分之好用,再也不用担心tar后面该加哪些参数了)的命令解压,然后出现了一个文件,本以为解压后是一个文件夹:然后一脸蒙逼~ 突然又想 ...

  7. Eclipse的SVN插件移动中文名称文件提示org.tigris.subversion.javahl.ClientException: Bogus URL

    今天一个同事使用Eclipse的SVN插件,在"SVN资源库"视图,移动一个中文名称的文件,提示org.tigris.subversion.javahl.ClientExcepti ...

  8. golang zip 压缩,解压(含目录文件)

    每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...

  9. Flask保存或解压上传的文件

    import os import uuid import shutil import zipfile from flask import Flask, render_template, request ...

随机推荐

  1. USB Mass Storage大容量存储的基本知识

    http://www.crifan.com/files/doc/docbook/usb_disk_driver/release/htmls/ch02_msc_basic.html 目录 2.1. US ...

  2. [置顶] 获取系统时间的方法--linux

    一. localtime 函数获取(年/月/日/时/分/秒)数值. 1.感性认识 #include<time.h>     //C语言的头文件 #include<stdio.h> ...

  3. OC基础13:数字、字符串和集合2

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 17.Foundation框架的数组是有序 ...

  4. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  5. 小心ThreadLocal的陷阱

    ThreadLocal用在多线程时保存线程级的局部变量,当我们需要在线程内共享数据时,ThreadLocal屡试不爽,但是ThreadLocal也会有一个问题,当你使用线程池时,线程可能会被重用,所以 ...

  6. maven01 hello maven

    安装省略,注意jdk的版本1.7: 目录:

  7. NSDictionary json格式字符串转字典,字典转json格式字符串

    + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return ...

  8. VC++客户端 缩小包的尺寸

    图片压缩: 1. 背景图片尺寸一般为100-300KB,使用纯色图片可缩小至1-3KB,方法得当可缩小1M多: 2. .ico一般为100-200KB,16x16一直到256x256,可减去其中几种尺 ...

  9. 使用微信 SDK 上传图片到七牛

    总体思路是:在微信下选好图片后将图片上传到微信服务器,在后端使用微信服务器返回的图片 serverId 加上调用接口的 ApiTicket 通过七牛的 fetch 接口向微信服务器下载多媒体文件的接口 ...

  10. NoSQL的价值到底在哪里?

    关系型数据库的价值 持久化数据:通过数据库来保存数据 处理并发:通过事务方式处理并发 集成:共享数据库集成,多个应用程序可以同时访问同一份数据 标准模型:前几种功能已经成标准,开发人员学习成本低,虽然 ...