Zip文件压缩与解压
1 Maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.soqi</groupId>
<artifactId>cn.soqi.img</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--jai图片处理-->
<dependency>
<groupId>javax.media.jai</groupId>
<artifactId>com.springsource.javax.media.jai.codec</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jmf</artifactId>
<version>2.1.1e</version>
</dependency>
<!--jai图片处理end-->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
</project>
2 压缩成zip文件
package cn.soqi.img;/**
* Created by user on 2017/5/8.
*/
/**
* ZIP文件压缩工具类
*
* @author brozer
* @email 798121446@qq.com
* @create 2017-05-08 9:53
*/
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import java.io.*;
//使用org.apache.tools.zip这个就不会中文乱码
//使用java.util.zip原生ZipOutputStream与ZipEntry会中文乱码
//import java.util.zip.ZipOutputStream;
//import java.util.zip.ZipEntry;
public class ZipCompress {
static String filePath = "C:\\Users\\user\\Pictures\\png";//需要压缩的文件夹完整路径
static String fileName = "png";//需要压缩的文件夹名
static String outPath = "C:/Users/user/Desktop/zip.zip";//压缩完成后保存为Test.zip文件,名字随意
public static void main(String args[]) throws Exception
{
OutputStream is = new FileOutputStream(outPath);//创建Test.zip文件
// CheckedOutputStream cos = new CheckedOutputStream(is, new CRC32());//检查输出流,采用CRC32算法,保证文件的一致性
ZipOutputStream zos = new ZipOutputStream(is);//创建zip文件的输出流
zos.setEncoding("GBK");//设置编码,防止中文乱码
File file = new File(filePath);//需要压缩的文件或文件夹对象
ZipFile(zos,file);//压缩文件的具体实现函数
zos.close();
// cos.close();
is.close();
System.out.println("压缩完成");
}
//递归,获取需要压缩的文件夹下面的所有子文件,然后创建对应目录与文件,对文件进行压缩
public static void ZipFile(ZipOutputStream zos,File file) throws Exception
{
if(file.isDirectory())
{
//创建压缩文件的目录结构
String filePath=file.getPath();
zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))+File.separator));
for(File f : file.listFiles())
{
ZipFile(zos,f);
}
}
else
{
//打印输出正在压缩的文件
System.out.println("正在压缩文件:"+file.getName());
//创建压缩文件
zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))));
//用字节方式读取源文件
InputStream is = new FileInputStream(file.getPath());
//创建一个缓存区
BufferedInputStream bis = new BufferedInputStream(is);
//字节数组,每次读取1024个字节
byte [] b = new byte[1024];
//循环读取,边读边写
while(bis.read(b)!=-1)
{
zos.write(b);//写入压缩文件
}
//关闭流
bis.close();
is.close();
}
}
}
3 解压Zip文件
package cn.soqi.img;/**
* Created by user on 2017/5/8.
*/
/**
* Zip文件解压工具类
*
* @author brozer
* @email 798121446@qq.com
* @create 2017-05-08 10:27
*/
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import java.io.*;
import java.util.Enumeration;
//使用org.apache.tools.zip这个就不会中文乱码
//使用java.util.zip原生ZipOutputStream与ZipEntry会中文乱码
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
public class ZipDeCompress
{
static String zipPath = "C:/Users/user/Desktop/zip.zip";//需要解压的压缩文件
static String outPath = "C:/Users/user/Desktop/";//解压完成后保存路径,记得"\\"结尾哈
public static void deCompress(String zipPath,String outPath) throws Exception
{
zipPath=zipPath.replace("\\","/");
outPath=outPath.replace("\\","/");
if(!outPath.endsWith("/"))
outPath=outPath+"/";
ZipFile zipFile = new ZipFile(zipPath,"GBK");//压缩文件的实列,并设置编码
//获取压缩文中的所以项
for(Enumeration<ZipEntry> enumeration = zipFile.getEntries();enumeration.hasMoreElements();)
{
ZipEntry zipEntry = enumeration.nextElement();//获取元素
//排除空文件夹
if(!zipEntry.getName().endsWith(File.separator))
{
System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息
//创建解压目录
String zipEntryStr=zipEntry.getName();
String fileNamePart=zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1,zipEntry.getName().length());
if(fileNamePart.trim().equals("")){
File f = new File(outPath+zipEntry.getName().substring(0, zipEntry.getName().lastIndexOf("/")));
//判断是否存在解压目录
if(!f.exists())
{
f.mkdirs();//创建解压目录
}
}else {
OutputStream os = new FileOutputStream(outPath + zipEntry.getName());//创建解压后的文件
BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流
InputStream is = zipFile.getInputStream(zipEntry);//读取元素
RgbToCmyk.rgbToCmyk(is,outPath+zipEntry.getName()); //解压文件,同时转换生成对应的CMYK文件
BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流
// CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保证文件的一致性
byte[] b = new byte[1024];//字节数组,每次读取1024个字节
//循环读取压缩文件的值
while (bis.read(b) != -1) {
bos.write(b);//写入到新文件
}
bis.close();
// cos.close();
is.close();
bos.close();
os.close();
}
}
else
{
//如果为空文件夹,则创建该文件夹
new File(outPath+zipEntry.getName()).mkdirs();
}
}
System.out.println("解压完成");
zipFile.close();
}
public static void main(String[] args) throws Exception {
deCompress(zipPath,outPath);
}
}