Java--解压缩zip包
Test.java
import java.io.IOException; public class Test {
public static void main(String[] args) throws IOException, Exception {
String srcPath = "E:\\zhanglong.zip";
String dstPath = "E:\\zhanglong";
Decompression.unZip(srcPath, dstPath);
}
}
Decompression.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; public class Decompression { //解压gzip数据
public static String unGzip(InputStream is){
String result = null;
GZIPInputStream in = null;
ByteArrayOutputStream arrayOutputStream = null;
try {
in = new GZIPInputStream(is);
arrayOutputStream = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[Constant.BUFFER];
while ((len = in.read(buffer)) != -1) {
arrayOutputStream.write(buffer, 0, len);
}
result = new String(arrayOutputStream.toByteArray(), Constant.UTF);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
if(arrayOutputStream != null){
arrayOutputStream.close();
}
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} //压缩gzip数据
public static byte[] gzip(String data, String charset) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data.getBytes(charset));
gzip.finish();
gzip.close();
b = bos.toByteArray();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
} /**************************************************/
//解压zip压缩包
public static boolean unZip(String srcPath, String dstPath) {
boolean isUnzipSuccess = false;
boolean isDstFormat = false;
int count = -1;
int index = -1;
File dstFolder = new File(dstPath);
if(!dstFolder.exists()) dstFolder.mkdirs();
FileInputStream fis = null;
ZipInputStream zis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
ZipEntry entry = null;
fis = new FileInputStream(srcPath);
zis = new ZipInputStream(new BufferedInputStream(fis)); while ((entry = zis.getNextEntry()) != null) {
byte[] data = new byte[Constant.BUFFER * 10];
String temp = entry.getName();
isDstFormat = isDstFormat(temp);
if(!isDstFormat)
continue; index = temp.lastIndexOf("/");
if (index > -1) temp = temp.substring(index + 1);
temp = dstPath + File.separator + temp; File file = new File(temp);
file.createNewFile(); fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, Constant.BUFFER * 10); while ((count = zis.read(data, 0, Constant.BUFFER * 10)) != -1) {
bos.write(data, 0, count);
} bos.flush();
}
isUnzipSuccess = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(bos != null) bos.close();
if(fos != null) fos.close();
if(zis != null) zis.close();
if(fis != null) fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return isUnzipSuccess;
} public static boolean isDstFormat(String filename){
boolean isDstFormat = false;
boolean isEndWithOgg = filename.endsWith(Constant.OGG);
boolean isEndWidthPack = filename.endsWith(Constant.PACK);
boolean isEndWidthPng = filename.endsWith(Constant.PNG);
boolean isEndWidthMp3 = filename.endsWith(Constant.MP3);
boolean isEndWidthTmx = filename.endsWith(Constant.TMX);
boolean isEndWidthProperties = filename.endsWith(Constant.PROPERTIES);
if( isEndWithOgg || isEndWidthPack || isEndWidthMp3 || isEndWidthPng || isEndWidthTmx || isEndWidthProperties)
isDstFormat = true; return isDstFormat;
}
}
FileOperate.java
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.ArrayList;
import java.util.LinkedList;
import java.util.List; public class FileOperate {
/**
* 在指定路径下创建新文件
* @param filePath 文件路径
* @return
* @throws IOException
*/
public static File createFile(String filePath, boolean isDeleteAllFiles) {
String parentPath = filePath.substring(0, filePath.lastIndexOf(File.separator));
createFolders(parentPath, isDeleteAllFiles);
File file = new File(filePath);
try {
if(!file.createNewFile()){
file.delete();
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
} /**
* 判断配置文件是否存在
* @param filePath 文件路径
* @return
*/
public static boolean isFileExist(String filePath){
File file = new File(filePath);
return file.exists();
} /**
* 取得指定目录下的所有文件夹名
* @return
*/
public static List<String> getFilesName(String filePath){
List<String> files_name = null;
File file = new File(filePath);
if(file.exists()){
files_name = new ArrayList<String>();
File[] files = file.listFiles();
for (File file2 : files) {
if(file2.isDirectory()){
files_name.add(file2.getName());
}
}
}
return files_name;
} public static void deleteFiles(File file){
if(file.exists()){
File[] files = file.listFiles();
for(File f : files){
if(f.isFile()) f.delete();
}
}
} public static File createFolders(String path, boolean isDeleteAllFiles){
File file = new File(path);
if(file.exists() && file.isDirectory()){
if(isDeleteAllFiles) deleteFiles(file);
}else{
file.mkdirs();
}
return file;
} public static boolean deleteFile(String filePath){
boolean isDeleted = false;
File file = new File(filePath);
if(file.exists()){
if(file.isFile()) {
file.delete();
isDeleted = true;
}
}
return isDeleted;
} public static void deleteMkdir(String filePath){
File file = new File(filePath);
if(file.exists() && file.isDirectory()){
file.delete();
}
} /*********删除文件夹******/
public static boolean deleteFolders(String filePath){
boolean isDeleteSuccess = false;
LinkedList<String> folderList = new LinkedList<String>();
folderList.add(filePath); while(folderList.size() > 0){
File file1 = new File(folderList.poll());
File[] files1 = file1.listFiles();
ArrayList<File> fileList = new ArrayList<File>();
for(int i = 0; i < fileList.size(); i++){
if(files1[i].isDirectory()){
folderList.add(files1[i].getPath());
}else{
fileList.add(files1[i]);
}
}
//删除文件
for(File file : fileList){
file.delete();
}
} //删除文件夹
folderList = new LinkedList<String>();
folderList.add(filePath);
while(folderList.size() > 0){
File file2 = new File(folderList.getLast());
if(file2.delete()){
folderList.removeLast();
}else{
File[] files2 = file2.listFiles();
for(int i = 0; i < files2.length; i++){
folderList.add(files2[i].getPath());
}
}
}
if(folderList.size() == 0) isDeleteSuccess = true;
return isDeleteSuccess;
} public static boolean moveFile(String srcFilePath, String dstFilePath){
boolean isMoveFileSuccess = false; BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
if(isFileExist(dstFilePath)) deleteFile(dstFilePath);
File dstFile = createFile(dstFilePath, false);
fis = new FileInputStream(new File(srcFilePath));
fos = new FileOutputStream(dstFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos, Constant.BUFFER * 10);
int count = -1;
byte[] buffer = new byte[Constant.BUFFER * 10];
while((count = bis.read(buffer, 0, Constant.BUFFER * 10)) != -1){
bos.write(buffer, 0, count);
bos.flush();
}
isMoveFileSuccess = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bos != null){
bos.close();
}
if(fos != null){
fos.close();
}
if(bis != null){
bis.close();
}
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return isMoveFileSuccess;
}
}
Constant.java
public class Constant {
public static final int BUFFER = 1024;
public static final String UTF = "utf-8"; public static final String OGG = ".ogg";
public static final String PACK = ".pack";
public static final String PNG = ".png";
public static final String MP3 = ".mp3"; public static final String PROPERTIES = ".properties";
public static final String TMX = ".tmx";
}
测试包:
http://download.csdn.net/detail/zlqqhs/5943973
Java--解压缩zip包的更多相关文章
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- java生成zip包兼容Linux
/* 这个方法只用在windows中用服务器为Linux就不行 */ package common.util; import java.io.File;import java.io.FileInput ...
- java解压缩zip和rar的工具类
package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...
- java解压缩zip
依赖的包: <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <gr ...
- java 解压缩Zip文件 ziputil
package com.lanyuan.assembly.util; import java.io.BufferedOutputStream;import java.io.File;import ja ...
- java zip API实现压缩和解压缩zip包
package zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io ...
- java中ant包中的org.apache.tools.zip实现压缩和解压缩
其实apache中的ant包(请自行GOOGLE之ant.jar)中有一个更好的类,已经支持中文了,我们就不重复制造轮子了,拿来用吧,这里最主要的功能是实现了 可以指定多个文件 到同一个压缩包的功能 ...
- IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.htmlJava API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹
一.简述 zip用于压缩和解压文件.使用到的类有:ZipEntry ZipOutputStream 二.具体实现代码 package com.joyplus.test; import java.io ...
随机推荐
- python抓包模块
pcapy模块 安装 yum install -y epel-release yum install -y pip gcc gcc-c++ libpcap-devel python-deve ...
- Jquery API学习笔记
学习网站 JQuery API 中文网: http://www.jquery123.com/ 学习一遍API可以更熟练的运用jquery并且拓展思路. 这里只挑选了一些我认为在开发中会用到的一些API ...
- [Luogu4724][模板]三维凸包(增量构造法)
1.向量点积同二维,x1y1+x2y2+x3y3.向量叉积是行列式形式,(y1z2-z1y2,z1x2-x1z2,x1y2-y1x2). 2.增量构造法: 1)首先定义,一个平面由三个点唯一确定.一个 ...
- [Agc002E]Candy Piles
[Agc002E]Candy Piles 题目大意 有\(n\)个数,两人轮流操作,可以做以下操作之一: 删掉一个最大的数 将所有数-1 最后取没的人输,问先手是否必胜? 试题分析 直接决策不知道选哪 ...
- python知识(2)----python的多态
python不支持多态,但他是一种多态语言,看下面这篇博客: 参考资料: [1]. 再谈python中的多态
- Android WebView H5开发拾遗
上篇介绍了一些WebView的设置,本篇为一些补充项. 首先加载HTML5的页面时,会出现页面空白的现象,原因是需要开启 DOM storage API 功能: webSettings.setDomS ...
- POJ 3580 SuperMemo (splay tree)
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ...
- php 利用fsockopen GET/POST 提交表单及上传文件
1.GET get.php <?php $host = 'demo.fdipzone.com'; $port = 80; $errno = ''; $errstr = ''; $timeout ...
- vim 之cscope的使用
http://www.mcuos.com/thread-8488-1-1.html http://blog.csdn.net/longerzone/article/details/7789581 ht ...
- druid连接数据库加解密
<bean id="dataSource" class="cn.zsmy.palmdoctor.utils.DecryptDruidSource" ini ...