概述
packagecom.ricoh.rapp.ezcx.admintoolweb.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.nio.charset.Charset;importjava.util.ArrayList;importjava.util.Enumeration;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;importjava.util.zip.ZipOutputStream;public classZipUtil {private static final int BUFFER_SIZE = 2 * 1024;/***@paramsrcDir
* 需要压缩的文件夹
*@paramzipPath
* 压缩文件目录
*@paramzipFileName
* 压缩文件的名称
*@throwsRuntimeException
*@throwsFileNotFoundException*/
public static voidtoZip(String srcDir, String zipPath, String zipFileName)throwsRuntimeException, FileNotFoundException {long start =System.currentTimeMillis();
File zipDir= newFile(zipPath);if (!zipDir.exists() || !zipDir.isDirectory()) {
zipDir.mkdirs();
}
File zipFile= newFile(zipPath, zipFileName);
FileOutputStream out= newFileOutputStream(zipFile);
ZipOutputStream zos= null;try{
zos= newZipOutputStream(out);
File sourceFile= newFile(srcDir);
compress(sourceFile, zos, sourceFile.getName(),false);long end =System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
}catch(Exception e) {throw new RuntimeException("zip error from ZipUtils", e);
}finally{if (zos != null) {try{
zos.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (out != null) {try{
out.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/***@paramsourceFile
* 需要压缩的文件目录
*@paramzos
* zip输出流
*@paramname
* 压缩后的zip名称
*@paramKeepDirStructure
* 是否保留原来的目录结构(false:不保留;true:保留)
*@throwsException*/
private static void compress(File sourceFile, ZipOutputStream zos, String name, booleanKeepDirStructure)throwsException {byte[] buf = new byte[BUFFER_SIZE];if(sourceFile.isFile()) {
zos.putNextEntry(newZipEntry(name));intlen;
FileInputStream in= newFileInputStream(sourceFile);while ((len = in.read(buf)) != -1) {
zos.write(buf,0, len);
}
zos.closeEntry();
in.close();
}else{
File[] listFiles=sourceFile.listFiles();if (listFiles == null || listFiles.length == 0) {
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
}else{for(File file : listFiles) {if(KeepDirStructure) {
compress(file, zos, name+ "/" + file.getName(), true);
}else{
compress(file, zos, file.getName(),true);
}
}
}
}
}/*** 压缩成ZIP 方法2
*
*@paramsrcFiles
* 需要压缩的文件列表
*@paramout
* 压缩文件输出流
*@throwsRuntimeException
**/
public static void toZip(File[] files, String zipPath, String zipFileName) throwsRuntimeException {long start =System.currentTimeMillis();
ZipOutputStream zos= null;
FileOutputStream out= null;try{
File zipDir= newFile(zipPath);if (!zipDir.exists() || !zipDir.isDirectory()) {
zipDir.mkdirs();
}
File zipFile= newFile(zipPath, zipFileName);
out= newFileOutputStream(zipFile);
zos= newZipOutputStream(out);for(File srcFile : files) {byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(newZipEntry(srcFile.getName()));intlen;
FileInputStream in= newFileInputStream(srcFile);while ((len = in.read(buf)) != -1) {
zos.write(buf,0, len);
}
zos.closeEntry();
in.close();
}long end =System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
}catch(Exception e) {throw new RuntimeException("zip error from ZipUtils", e);
}finally{if (zos != null) {try{
zos.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (out != null) {try{
out.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 解压文件到指定目录*/@SuppressWarnings("rawtypes")public static voidunZipFiles(String zipPath, String descDir) {
File zipFile= newFile(zipPath);
File pathFile= newFile(descDir);if (!pathFile.exists()) {
pathFile.mkdirs();
}//解决zip文件中有中文目录或者中文文件
ZipFile zip = null;try{
zip= new ZipFile(zipFile, Charset.forName("GBK"));for (Enumeration entries =zip.entries(); entries.hasMoreElements();) {//输出文件路径信息
FileOutputStream out = null;
InputStream in= null;try{
ZipEntry entry=(ZipEntry) entries.nextElement();
String zipEntryName=entry.getName();
in=zip.getInputStream(entry);
String outPath= descDir + File.separator +zipEntryName;//判断路径是否存在,不存在则创建文件路径//File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));if (!file.exists()) {
file.mkdirs();
}//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (newFile(outPath).isDirectory()) {continue;
}
out= newFileOutputStream(outPath);byte[] buf1 = new byte[1024];intlen;while ((len = in.read(buf1)) > 0) {
out.write(buf1,0, len);
}
}catch(Exception e) {
e.printStackTrace();
}finally{if (out != null) {try{
out.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (in != null) {try{
in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}catch(IOException e1) {
e1.printStackTrace();
}finally{if (zip != null) {try{
zip.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 测试*/
public static void main(String[] args) throwsFileNotFoundException {/** String sourFile = "D:/testzip"; String zipPath = "D:/wl"; String zipFileName
* = "tow.zip"; ZipUtil.toZip(sourFile, zipPath, zipFileName);*/
/** File sfile = new File(sourFile); List fileList = new ArrayList<>();
* for(File f : sfile.listFiles()) { fileList.add(f); } toZip(sfile.listFiles(),
* zipPath, zipFileName);*/String zipPath1= "d:/testunzip/user.zip";
String zipPath2= "D:/testunzip/photos.zip";
String descDir1= "d:/testunzip/";
String descDir2= "d:/testunzip/ps";
unZipFiles(zipPath2, descDir2);
}
}
最后
以上就是称心店员为你收集整理的java 解压缩 工具类_java中自己常用到的工具类-压缩解压zip文件的全部内容,希望文章能够帮你解决java 解压缩 工具类_java中自己常用到的工具类-压缩解压zip文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复