压缩:
package com.sichang.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 压缩文件
* @author Administrator
*
*/
public class ZipOutputStreamUn {
// public static void main(String args[]) throws IOException {
//// test1();
// String AgoFileName="D:\\test";
// String AfterFilePath="D:\\test.zip";
// ManyFile(AgoFileName,AfterFilePath);
// }
/**
* 多文件压缩
* AgoFileName 需要压缩的文件路径
* AfterFilePath 压缩后的文件路径
*
* String AgoFileName="D:\\test";
* String AfterFilePath="D:\\test.zip";
* ManyFile(AgoFileName,AfterFilePath);
*
* @throws IOException
*/
public static void ManyFile(String AgoFileName, String AfterFilePath) throws IOException {
File inFile = new File(AgoFileName);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(AfterFilePath));
//zos.setComment("多文件处理");
zipFile(inFile, zos, "");
zos.close();
}
/**
* 单个文件压缩
* @throws IOException
public static void SingleFile() throws IOException {
// 压缩后存储路径
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\testZip.zip"), Charset.forName("GBK"));
//实例化一个名称为ab.txt的ZipEntry对象
ZipEntry entry = new ZipEntry("Service.txt");// 压缩包里面文件的名称
//设置注释
zos.setComment("zip测试for单个文件");
//把生成的ZipEntry对象加入到压缩文件中,而之后往压缩文件中写入的内容都会放在这个ZipEntry对象里面
zos.putNextEntry(entry);
InputStream is = new FileInputStream("D:\\Service.txt");
int len = 0;
while ((len = is.read()) != -1)
zos.write(len);
is.close();
zos.close();
}
*/
public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException {
if (inFile.isDirectory()) {
File[] files = inFile.listFiles();
for (File file:files)
zipFile(file, zos, dir + "\\" + inFile.getName());
} else {
String entryName = null;
if (!"".equals(dir))
entryName = dir + "\\" + inFile.getName();
else
entryName = inFile.getName();
ZipEntry entry = new ZipEntry(entryName);
zos.putNextEntry(entry);
InputStream is = new FileInputStream(inFile);
int len = 0;
while ((len = is.read()) != -1)
zos.write(len);
is.close();
}
}
}
解压缩:
package com.sichang.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
* 解压缩文件
* @author Administrator
*
*/
public class ZipInputStreamUn {
// public static void main(String args[]) throws Exception {
// String AgoFileName="D:\\test.zip";
// String AfterFilePath="D:\\unpackTest\\";
// Unzip(AgoFileName,AfterFilePath);
// }
/**
* 这个方法有缺陷,不能使用!!!!!!!!!!!
* AgoFileName 需要解压的文件路径
* AfterFilePath 解压后的文件路径
*
* String AgoFileName="D:\\test.zip";
* String AfterFilePath="D:\\unpackTest\\";
* Unzip(AgoFileName,AfterFilePath);
*
* @throws Exception
*/
public static void Unzip(String AgoFileName, String AfterFilePath) throws Exception{
System.out.println("开始解压缩文件-----------------");
File file = new File(AgoFileName);//压缩文件
ZipFile zipFile = new ZipFile(file);//实例化ZipFile,每一个zip压缩文件都可以表示为一个ZipFile
//实例化一个Zip压缩文件的ZipInputStream对象,可以利用该类的getNextEntry()方法依次拿到每一个ZipEntry对象
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String fileName = zipEntry.getName();
System.out.println(fileName);
File temp = new File(AfterFilePath + fileName);
if (!temp.getParentFile().exists())
temp.getParentFile().mkdirs();
OutputStream os = new FileOutputStream(temp);
//通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
InputStream is = zipFile.getInputStream(zipEntry);
int len = 0;
while ((len = is.read()) != -1)
os.write(len);
os.close();
is.close();
}
zipInputStream.close();
}
/**
* 解压到指定目录
* @param zipPath
* @param descDir
* @author isea533
*/
public static void unZipFiles(String zipPath,String descDir)throws IOException{
// 接收字符串后转为File
unZipFiles(new File(zipPath), descDir);
}
/**
* 解压文件到指定目录
* AgoFileName 需要解压的文件路径
* AfterFilePath 解压后的文件路径
*
* String AgoFileName="D:\\test.zip";
* String AfterFilePath="D:\\unpackTest\\";
* Unzip(AgoFileName,AfterFilePath);
*/
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile,String descDir)throws IOException{
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries = zip.entries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
//输出文件路径信息
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
System.out.println("******************解压完毕********************");
}
}