java 压缩文件夹_java 实现压缩文件(单文件 或 文件夹)
接著上篇了解一下java壓縮實現(xiàn)過程,下面的是支持 單文件 或 文件夾 壓縮的實現(xiàn),使用遞歸。
效果:
代碼:
package com.gx.compress;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @ClassName: CompressDirUtil
* @Description: 壓縮文件工具類
* @author zhoujie
* @date 2018年7月29日 下午9:08:44
* @version V1.0
*/
public class CompressDirUtil {
static String compresspath = "F:\\圖片\\轉(zhuǎn)換圖片"; //需要壓縮的文件夾的目錄
public static void main(String[] args) {
boolean bl = compressFileToZip(compresspath); //壓縮文件
if(bl){
System.out.println("壓縮成功");
}
}
/**
* @Title: compressAllFileZip
* @Description: 傳遞文件路徑壓縮文件,傳遞文件夾路徑壓縮文件夾,注:空的文件夾不會出現(xiàn)在壓縮包內(nèi)
* @param @param compresspath 需要壓縮的文件夾的目錄
* @return void 返回類型
* @throws
*/
public static boolean compressFileToZip(String compresspath) {
boolean bool = false;
try {
ZipOutputStream zipOutput = null;
File file = new File(compresspath);
if(file.isDirectory()){
zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath + ".zip")));
compressZip(zipOutput, file, ""); //遞歸壓縮文件夾,最后一個參數(shù)傳""壓縮包就不會有當前文件夾;傳file.getName(),則有當前文件夾;
}else{
zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath.substring(0, compresspath.lastIndexOf(".")) + ".zip")));
zipOFile(zipOutput, file); //壓縮單個文件
}
zipOutput.closeEntry();
zipOutput.close();
bool = true;
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
/**
* @Title: compressZip
* @Description: 子文件夾中可能還有文件夾,進行遞歸
* @param @param zipOutput
* @param @param file
* @param @param suffixpath
* @param @throws IOException
* @return void 返回類型
* @throws
*/
private static void compressZip(ZipOutputStream zipOutput, File file, String suffixpath) {
File[] listFiles = file.listFiles();// 列出所有的文件
for(File fi : listFiles){
if(fi.isDirectory()){
if(suffixpath.equals("")){
compressZip(zipOutput, fi, fi.getName());
}else{
compressZip(zipOutput, fi, suffixpath + File.separator + fi.getName());
}
}else{
zip(zipOutput, fi, suffixpath);
}
}
}
/**
* @Title: zip
* @Description: 壓縮的具體操作
* @param @param zipOutput
* @param @param file 文件
* @param @param suffixpath 文件夾拼接路徑
* @return void 返回類型
* @throws
*/
public static void zip(ZipOutputStream zipOutput, File file, String suffixpath) {
try {
ZipEntry zEntry = null;
if(suffixpath.equals("")){
zEntry = new ZipEntry(file.getName());
}else{
zEntry = new ZipEntry(suffixpath + File.separator + file.getName());
}
zipOutput.putNextEntry(zEntry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Title: zip
* @Description: 壓縮單個文件
* @param @param zipOutput
* @param @param file 文件
* @return void 返回類型
* @throws
*/
public static void zipOFile(ZipOutputStream zipOutput, File file) {
try {
ZipEntry zEntry = new ZipEntry(file.getName());
zipOutput.putNextEntry(zEntry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ok。
總結(jié)
以上是生活随笔為你收集整理的java 压缩文件夹_java 实现压缩文件(单文件 或 文件夹)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浙商银行VISA标准卡额度是多少?怎么提
- 下一篇: 追求高收益理财损失本金的案例,不可不慎重