http://blog.csdn.net/lanpiao_87/article/details/7310461
- package com.lanp;
-
- 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.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
-
- public final class FileToZip {
-
- private FileToZip() {
-
- }
-
-
- public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) {
- boolean flag = false;
- File sourceFile = new File(sourceFilePath);
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- FileOutputStream fos = null;
- ZipOutputStream zos = null;
-
- if(sourceFile.exists() == false) {
- 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++) {
-
- ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
- zos.putNextEntry(zipEntry);
-
- 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;
- }
-
-
- public static void main(String[] args) {
- String sourceFilePath = "C:\\home\\lp20120301";
- String zipFilePath = "C:\\home";
- String fileName = "lp20120301";
- boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
- if(flag) {
- System.out.println(">>>>>> 文件打包成功. <<<<<<");
- } else {
- System.out.println(">>>>>> 文件打包失败. <<<<<<");
- }
- }
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
2013-11-07 jQuery select操作控制方法小结
2012-11-07 pb调用vc写的动态链接库文件
2012-11-07 vs2008下MFC内存泄露问题一点经验
2012-11-07 左值的理解(给渴望学习的新手)
2012-11-07 C++指针学习心得
2012-11-07 c++ 指针精髓
2012-11-07 C/C++中printf和C++中cout的输出格式