导语
接到一个需求,说是要把文件夹打包成成zip包,还要求使用Java代码来实现,想想既然是工具类,那么就来编写一个工具来试试。
废话不多说直接上代码
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Arrays;import java.util.LinkedList;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;/*** 文件夹打压缩包** @author ZYGisComputer*/public final class FileTOZip {/*** 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。** @param sourceFilePath 待压缩的文件路径* @param zipFilePath 压缩后存放路径* @param fileName 压缩后文件的名称* @return flag*/public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {String fileNameExit=sourceFilePath+".zip";File file=new File(fileNameExit);if(file.exists()){file.delete();}boolean flag = false;File sourceFile = new File(sourceFilePath);FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;ZipOutputStream zos = null;if (!sourceFile.exists()) {System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");} else {try {File zipFile = new File(zipFilePath + "/" + fileName + ".zip");if (zipFile.exists()) {System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:"+ fileName + ".zip" + " 打包文件. <<<<<<");} else {File[] sourceFiles = sourceFile.listFiles();if (null == sourceFiles || sourceFiles.length < 1) {System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath+ " 里面不存在文件,无需压缩. <<<<<<");} else {fos = new FileOutputStream(zipFile);zos = new ZipOutputStream(new BufferedOutputStream(fos));byte[] bufs = new byte[1024 * 10];for (int i = 0; i < sourceFiles.length; i++) {// 创建ZIP实体,并添加进压缩包ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());zos.putNextEntry(zipEntry);sourceFiles[i].setExecutable(true);sourceFiles[i].setReadable(true);sourceFiles[i].setWritable(true);// 读取待压缩的文件并写进压缩包里fis = new FileInputStream(sourceFiles[i]);bis = new BufferedInputStream(fis, 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {zos.write(bufs, 0, read);}}flag = true;}}} catch (FileNotFoundException e) {e.printStackTrace();throw new RuntimeException(e);} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);} finally {// 关闭流try {if (null != bis) {bis.close();}if (null != zos) {zos.close();}} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}}}return flag;}/*** 将文件打包成ZIP压缩文件,main方法测试** @param args*/public static void main(String[] args) {String sourceFilePath = "E:\\ruoyi\\uploadPath\\upload\\200";String zipFilePath = "E:\\ruoyi\\uploadPath\\upload";String fileName = "200";boolean flag = FileTOZip.fileToZip(sourceFilePath, zipFilePath,fileName);if (flag) {System.out.println(">>>>>> 文件打包成功. <<<<<<");} else {System.out.println(">>>>>> 文件打包失败. <<<<<<");}}public static void ZIPFile() throws Exception {String sourceDir = "E:\\ruoyi\\uploadPath\\upload";int parentDirectoryLen = sourceDir.lastIndexOf(File.separator) + 1;File[] copyfoldersList = new File(sourceDir).listFiles();FileOutputStream fos = new FileOutputStream("E:\\ruoyi\\uploadPath\\upload\\down.zip");ZipOutputStream zipOut = new ZipOutputStream(fos);for (int k = 0; k < copyfoldersList.length; k++) {if (copyfoldersList[k].isDirectory()) {LinkedList copysourcepath = new LinkedList(Arrays.asList(copyfoldersList[k].getAbsolutePath()));while (copysourcepath.size() > 0) {File folders = new File(copysourcepath.peek().toString());String[] file = folders.list();for (int i = 0; i < file.length; i++) {File ff = new File(copysourcepath.peek().toString(), file[i]);if (ff.isFile()) {FileInputStream fis = null;try {fis = new FileInputStream(ff);ZipEntry entry = new ZipEntry(ff.getAbsoluteFile().toString().substring(parentDirectoryLen));zipOut.putNextEntry(entry);int nNumber;Long len=Long.MIN_VALUE;byte[] buffer = new byte[len.intValue()];while ((nNumber = fis.read(buffer)) != -1)//System.out.println(buffer.toString());zipOut.write(buffer, 0, nNumber);} catch (IOException e) {e.printStackTrace();zipOut.close();fos.close();} finally {try {fis.close();} catch (IOException e) {}}} else if (ff.isDirectory()) {for (File f : ff.listFiles()) {if (f.isDirectory())copysourcepath.add(f.getPath());else if (f.isFile()) {FileInputStream fis = null;try {fis = new FileInputStream(f);ZipEntry entry = new ZipEntry(f.getAbsoluteFile().toString().substring(parentDirectoryLen));zipOut.putNextEntry(entry);int nNumber;Long len=Long.MIN_VALUE;byte[] buffer = new byte[len.intValue()];while ((nNumber = fis.read(buffer)) != -1)System.out.println(ff.getName());zipOut.write(buffer, 0, nNumber);} catch (IOException e) {e.printStackTrace();zipOut.close();fos.close();} finally {try {fis.close();} catch (IOException e) {}}}}}}copysourcepath.removeFirst();}}}try {zipOut.flush();} catch (IOException e) {e.printStackTrace();} finally {try {zipOut.close();fos.close();} catch (IOException e) {}}}}
如果觉得《Java实现文件夹打包》对你有帮助,请点赞、收藏,并留下你的观点哦!