java压缩解压文件

java

  利用Apache的commons-compress包实现对文件的压缩和解压

所需jar包:(代码中用到了文件拷贝,因此导入了commons-io包)

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-compress</artifactId>

<version>1.18</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.5</version>

</dependency>

具体实现:

public class ZipFileUtil {

/**

* 压缩文件

* @param srcFilePath 需要压缩的文件路径

* @param zipFilePath 压缩后文件(全路径的文件,例如:E:/test.zip)

* @throws Exception

*/

public static void compressFilesZip(String srcFilePath, String zipFilePath)throws Exception{

Date start = new Date();

System.out.println("压缩文件路径为:" + srcFilePath);

if (srcFilePath == null || "".equals(srcFilePath.trim())){

throw new Exception("压缩文件路径不能为空");

}

File srcFile = new File(srcFilePath);

if (!srcFile.exists()){

throw new Exception("文件目录不存在!");

}

File []files = srcFile.listFiles();

if (files == null || files.length <= 0){

throw new Exception("需压缩文件目录为空!");

}

List<String> allFileDir = getAllFiles(srcFilePath); //获取所有文件目录

File resultZipFile = new File(zipFilePath);

try(ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(resultZipFile)) {

zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);

//将每个文件用ZipArchiveEntry封装再写到压缩文件中

addAllFilesToZip(srcFilePath, allFileDir, zipArchiveOutputStream);

zipArchiveOutputStream.finish();

}

Date endDate = new Date();

System.out.println("压缩执行时间为:" + (endDate.getTime()-start.getTime()));

}

/**

* 把文件添加到压缩包中

* @param srcFilePath

* @param allFileDir

* @param zipArchiveOutputStream

* @throws IOException

*/

private static void addAllFilesToZip(String srcFilePath, List<String> allFileDir, ZipArchiveOutputStream zipArchiveOutputStream) throws IOException {

for (String fileDir : allFileDir) {

File file = new File(fileDir);

if (file.isDirectory()) { //若是目录则不压缩

continue;

}

//获取要压缩文件的相对路径,再将相对路径设置到压缩目录中完成对文件夹的压缩

String fileName = getFileRelativePathName(srcFilePath, fileDir);

ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, fileName);

zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);

try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {

IOUtils.copy(inputStream, zipArchiveOutputStream);

}

zipArchiveOutputStream.closeArchiveEntry();

}

}

/**

* 把zip解压到指定目录

* @param zipFilePath 压缩包路径

* @param saveFileDir 解压到目录

*/

public static void decompressZip(String zipFilePath, String saveFileDir)throws Exception{

System.out.println("解压文件为:" + zipFilePath);

Date start = new Date();

if (saveFileDir == null || "".equals(saveFileDir.trim())){

throw new Exception("解压目录不能为空");

}

File zipFile = new File(zipFilePath);

if (!zipFile.exists()){

throw new Exception("需解压的文件不存在!");

}

//解压目录不存在创建目录

File saveFilePath = new File(saveFileDir);

if (!saveFilePath.exists()){

saveFilePath.mkdirs();

}

try(InputStream inputStream = new FileInputStream(zipFile)){

try (ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(inputStream)) {

ArchiveEntry archiveEntry = null;

while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null){

//解压时拷贝文件目录如若不存在则创建目录

String catalog = saveFileDir + "/" + archiveEntry.getName();

catalog = catalog.substring(0,catalog.lastIndexOf("/"));

File catalogFile = new File(catalog);

if (!catalogFile.exists()){

catalogFile.mkdirs();

}

try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(saveFileDir,archiveEntry.getName())))){

IOUtils.copyLarge(zipArchiveInputStream,outputStream);

}finally {

IOUtils.closeQuietly();

}

}

}

}

Date endDate = new Date();

System.out.println("解压执行时间为:" + (endDate.getTime()-start.getTime()));

}

/**

* 获取相对路径

* @param dir

* @param path

* @return

*/

public static String getFileRelativePathName(String dir, String path){

if (!dir.endsWith("/")){

dir = dir + "/";

}

String temp = path.substring(dir.length());

return temp;

}

/**

* 递归获取目录中所有文件

* @param dir

* @return

*/

public static List<String> getAllFiles(String dir){

List<String> allDirs = new ArrayList<>();

File srcFile = new File(dir);

File files[] = srcFile.listFiles();

for (File file : files){

if (file.isDirectory()){

allDirs.add(file.getPath());

allDirs.addAll(getAllFiles(file.getPath()));

}else {

allDirs.add(file.getPath());

}

}

return allDirs;

}

//测试压缩与解压

public static void main(String args[])throws Exception{

compressFilesZip("E:/123","E:/123ABC.zip");

decompressZip("E:/123ABC.zip","E:/1");

}

}

 

以上是 java压缩解压文件 的全部内容, 来源链接: utcz.com/z/390727.html

回到顶部