123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- /**
- *
- * Copyright (c) behosoft Co.,Ltd.
- * All Rights Reserved.
- *
- * This software is the confidential and proprietary information of behosoft.
- * (Social Security Department). You shall not disclose such
- * Confidential Information and shall use it only in accordance with
- * the terms of the license agreement you entered into with behosoft.
- *
- * Distributable under GNU LGPL license by gnu.org
- */
- package com.behosoft.util;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.List;
- import java.util.zip.CRC32;
- import java.util.zip.CheckedOutputStream;
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
- /**
- * <p>
- * Title: behosoft_[子系统统名]_[模块名]
- * </p>
- * <p>
- * Description: zip文件的压缩和解压
- * </p>
- *
- * @author 蒋勇华
- * @version $Revision$ 2013-6-19
- * @author (lastest modification by $Author$)
- * @since 1.0
- */
- public class ZipUtils {
- public static final String EXT = ".zip";
- private static final String ENCODE = "gbk";
- private static final String BASE_DIR = "";
- private static final String PATH = File.separator;// 符号"/"用来作为目录标识判断符
- private static final int BUFFER = 8192;
- private static final long MAX_FILE_SIZE = 1610612736;// 单个压缩文件的最大大小 1.5G
- private static String getShortName(String fileName) {
- int index = fileName.indexOf(".");
- if (index > -1) {
- fileName = fileName.substring(0, index);
- }
- return fileName;
- }
- /**
- * 压缩
- *
- * @param srcFile
- * @throws Exception
- */
- public static void compress(File srcFile) throws Exception {
- String name = getShortName(srcFile.getName());
- String basePath = srcFile.getParent() + PATH;
- String destPath = basePath + name + EXT;
- compress(srcFile, destPath);
- }
- /**
- * 压缩单个文件或目录
- *
- * @param srcFile 源路径
- * @param destPath 目标路径
- * @throws Exception
- */
- public static void compress(File srcFile, File destFile) throws Exception {
- // 对输出文件做CRC32校验
- CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destFile), new CRC32());
- ZipOutputStream zos = new ZipOutputStream(cos);
- zos.setEncoding(ENCODE);
- compress(srcFile, zos, BASE_DIR);
- zos.flush();
- zos.close();
- }
- /***
- * <p>
- * Description:压缩多个文件或目录
- * </p>
- *
- * @param srcFiles
- * @param destFile
- * @throws Exception
- */
- public static void compress(File[] srcFiles, File destFile) throws Exception {
- // 对输出文件做CRC32校验
- CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destFile), new CRC32());
- ZipOutputStream zos = new ZipOutputStream(cos);
- zos.setEncoding(ENCODE);
- for (File srcFile : srcFiles) {
- compress(srcFile, zos, BASE_DIR);
- }
- zos.flush();
- zos.close();
- }
- /**
- * 压缩文件
- *
- * @param srcFile
- * @param destPath
- * @throws Exception
- */
- public static void compress(File srcFile, String destPath) throws Exception {
- compress(srcFile, new File(destPath));
- }
- /**
- * 压缩
- *
- * @param srcFile 源路径
- * @param zos ZipOutputStream
- * @param basePath 压缩包内相对路径
- * @throws Exception
- */
- private static void compress(File srcFile, ZipOutputStream zos, String basePath) throws Exception {
- if (srcFile.isDirectory()) {
- compressDir(srcFile, zos, basePath);
- }
- else {
- compressFile(srcFile, zos, basePath);
- }
- }
- /**
- * 压缩
- *
- * @param srcPath
- * @throws Exception
- */
- public static void compress(String srcPath) throws Exception {
- File srcFile = new File(srcPath);
- compress(srcFile);
- }
- /**
- * 文件压缩
- *
- * @param srcPath 源文件路径
- * @param destPath 目标文件路径
- */
- public static void compress(String srcPath, String destPath) throws Exception {
- File srcFile = new File(srcPath);
- compress(srcFile, destPath);
- }
- /**
- * 压缩目录
- *
- * @param dir
- * @param zos
- * @param basePath
- * @throws Exception
- */
- private static void compressDir(File dir, ZipOutputStream zos, String basePath) throws Exception {
- File[] files = dir.listFiles();
- // 构建空目录
- if (files.length < 1) {
- ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH);
- zos.putNextEntry(entry);
- zos.closeEntry();
- }
- for (File file : files) {
- // 递归压缩
- compress(file, zos, basePath + dir.getName() + PATH);
- }
- }
- /**
- * 文件压缩
- *
- * @param file 待压缩文件
- * @param zos ZipOutputStream
- * @param dir 压缩文件中的当前路径
- * @throws Exception
- */
- private static void compressFile(File file, ZipOutputStream zos, String dir) throws Exception {
- /**
- * 压缩包内文件名定义
- *
- * <pre>
- * 如果有多级目录,那么这里就需要给出包含目录的文件名
- * 如果用WinRAR打开压缩包,中文名将显示为乱码
- * </pre>
- */
- ZipEntry entry = new ZipEntry(dir + file.getName());
- zos.putNextEntry(entry);
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- int count;
- byte data[] = new byte[BUFFER];
- while ((count = bis.read(data, 0, BUFFER)) != -1) {
- zos.write(data, 0, count);
- }
- bis.close();
- zos.closeEntry();
- }
- private static File createCompressedDir(File sourceDir, File destDir) {
- File archiveDir = new File(destDir.getAbsolutePath() + File.separator + sourceDir.getName());
- if (!archiveDir.exists()) {
- archiveDir.mkdir();
- }
- return archiveDir;
- }
- /***
- * 分卷压缩目录
- *
- * @param sourcePath 源目录
- * @param destPath 压缩完成后放置到的目录
- * @return
- * @throws Exception
- */
- public static File volumeCompressDir(String sourcePath, String destPath) throws Exception {
- File sourceDir = new File(sourcePath);
- File destDir = new File(destPath);
- return volumeCompressDir(sourceDir, destDir);
- }
- /***
- * 分卷压缩目录
- *
- * @param sourceDir 源目录
- * @param destDir 压缩完成后放置到的目录
- * @return
- * @throws Exception
- */
- public static File volumeCompressDir(File sourceDir, File destDir) throws Exception {
- if (!sourceDir.isDirectory()) {
- throw new Exception("源路径不是有效的目录");
- }
- if (!destDir.isDirectory()) {
- throw new Exception("目标路径不是有效的目录");
- }
- File compressedDir = createCompressedDir(sourceDir, destDir);
- String basePath = compressedDir.getAbsolutePath() + File.separator;
- File[] list = sourceDir.listFiles();
- long compressedSize = 0;
- int volumeCount = 1;
- ZipOutputStream zos = null;
- for (File file : list) {
- if (file.isFile()) {
- if (zos == null) {
- CheckedOutputStream cos =
- new CheckedOutputStream(new FileOutputStream(basePath + sourceDir.getName() + "_"
- + volumeCount + EXT), new CRC32());
- zos = new ZipOutputStream(cos);
- zos.setEncoding(ENCODE);
- compressedSize = addVolumeCompressFile(file, zos).getSize();
- }
- else {
- if ((compressedSize + file.length()) >= MAX_FILE_SIZE) {
- if (zos != null) {
- zos.close();
- }
- compressedSize = 0;
- volumeCount = volumeCount + 1;
- CheckedOutputStream cos2 =
- new CheckedOutputStream(new FileOutputStream(basePath + sourceDir.getName() + "_"
- + volumeCount + EXT), new CRC32());
- zos = new ZipOutputStream(cos2);
- zos.setEncoding(ENCODE);
- compressedSize = compressedSize + addVolumeCompressFile(file, zos).getSize();
- }
- else {
- compressedSize = compressedSize + addVolumeCompressFile(file, zos).getSize();
- }
- }
- }
- else {
- // 不支持对子目录的压缩
- }
- }
- if (zos != null) {
- zos.close();
- }
- return compressedDir;
- }
- private static ZipEntry addVolumeCompressFile(File file, ZipOutputStream zos) throws Exception {
- ZipEntry entry = new ZipEntry(file.getName());
- zos.putNextEntry(entry);
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- int count;
- byte data[] = new byte[BUFFER];
- while ((count = bis.read(data, 0, BUFFER)) != -1) {
- zos.write(data, 0, count);
- zos.flush();
- }
- bis.close();
- zos.closeEntry();
- return entry;
- }
- /***
- * 分卷解压缩
- *
- * @param sourcePath
- * @param destPath
- * @return
- * @throws Exception
- */
- public static List<String> unCompressVolumeDir(String sourcePath, String destPath) throws Exception {
- File sourceDir = new File(sourcePath);
- File destDir = new File(destPath);
- return unCompressVolumeDir(sourceDir, destDir);
- }
- /***
- * 分卷解压缩
- *
- * @param sourceDir
- * @param destDir
- * @return
- * @throws Exception
- */
- public static List<String> unCompressVolumeDir(File sourceDir, File destDir) throws Exception {
- if (sourceDir.isFile()) {// 对zip文件的解压
- return uncompress(sourceDir.getAbsolutePath(), destDir.getAbsolutePath());
- }
- else {// 对zip分卷目录的解压
- List<String> filePaths = new ArrayList<String>();
- if (!sourceDir.isDirectory()) {
- throw new Exception("源路径不是有效的目录");
- }
- if (!destDir.isDirectory()) {
- throw new Exception("目标路径不是有效的目录");
- }
- File[] list = sourceDir.listFiles();
- for (File zipFile : list) {
- List<String> entries = uncompress(zipFile.getAbsolutePath(), destDir.getAbsolutePath());
- for (String s : entries) {
- filePaths.add(s);
- }
- }
- return filePaths;
- }
- }
- /***
- * 解开单个压缩文件
- *
- * @param zipFilePath
- * @param targetDir
- * @return
- * @throws Exception
- */
- public static List<String> uncompress(String zipFilePath, String targetDir) throws Exception {
- List<String> list = new ArrayList<String>();
- byte b[] = new byte[BUFFER];
- if (!targetDir.endsWith(File.separator)) {
- targetDir = targetDir + File.separator;
- }
- ZipFile zipFile = new ZipFile(zipFilePath, ENCODE);
- Enumeration<ZipEntry> entries = zipFile.getEntries();
- while (entries.hasMoreElements()) {
- ZipEntry zipEntry = entries.nextElement();
- String fileName = zipEntry.getName();
- File targetFolder = new File(targetDir);
- if (!targetFolder.exists()) {
- targetFolder.mkdir();
- }
- File temp = new File(targetDir + fileName);
- OutputStream os = new FileOutputStream(temp);
- // 通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
- InputStream is = zipFile.getInputStream(zipEntry);
- int len = 0;
- while ((len = is.read(b)) != -1) {
- os.write(b, 0, len);
- }
- os.close();
- is.close();
- list.add(targetDir + fileName);
- }
- return list;
- }
- /***
- * <p>
- * Description:解压
- * </p>
- *
- * @param zipFilePath
- * @return
- * @throws Exception
- */
- public static List<String> uncompress(String zipFilePath) throws Exception {
- File file = new File(zipFilePath);
- return uncompress(zipFilePath, file.getParent());
- }
- // public static void main(String[] args) {
- // try {
- // // 压缩单个
- // ZipUtils.compress("D:\\TEST\\abc一二三123.txt");
- // // 压缩多个
- // List<File> files = new ArrayList<File>();
- // files.add(new File("D:\\TEST\\WebappReport.jrxml"));
- // files.add(new File("D:\\TEST\\WebappReport.jasper"));
- // File destFile = new File("D:\\TEST\\WebappReport2.zip");
- // ZipUtils.compress(files.toArray(new File[files.size()]), destFile);
- // // 解压到指定目录
- // ZipUtils.uncompress("D:\\TEST\\WebappReport2.zip", "D:\\TEST\\a");
- // // 解压到当前目录
- // ZipUtils.uncompress("D:\\TEST\\abc一二三123.zip");
- // }
- // catch (Exception e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- // }
- }
|