java压缩文件工具类
来源:互联网 发布:c 高级编程第10版下载 编辑:程序博客网 时间:2024/06/07 08:01
- package tests;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- public class ZipUtil {
- private static File zip;
- private static ZipOutputStream zipOut;
- private InputStream input;
- public ZipUtil(){
- try {
- zipOut = new ZipOutputStream(new FileOutputStream(zip));
- zipOut.setComment("hello2");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void zipFile(File file) {
- if (file.isDirectory()) {
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (files[i].isDirectory()) {
- zipFile(files[i]);
- } else {
- try {
- input = new FileInputStream(files[i]);
- zipOut.putNextEntry(new ZipEntry(file.getName()
- + File.separator + files[i].getName()));
- int temp = 0;
- while ((temp = input.read()) != -1) {
- zipOut.write(temp);
- }
- input.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- try {
- zipOut.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- try {
- input = new FileInputStream(file);
- zipOut.putNextEntry(new ZipEntry(file.getName()));
- int temp = 0;
- while ((temp = input.read()) != -1) {
- zipOut.write(temp);
- }
- input.close();
- zipOut.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- zip = new File("d:" + File.separator + "hello.rar");
- File file = new File("d:" + File.separator + "hello");
- new ZipUtil().zipFile(file);
- }
- }
0 0
- java压缩文件工具类
- Java压缩文件生成工具类
- Java压缩文件生成工具类
- Java压缩文件的工具类
- Java压缩文件的工具类
- 压缩文件工具类
- 压缩文件工具类
- java实现将ZIP压缩文件解压的工具类
- java实现将ZIP压缩文件解压的工具类
- Java操作zip压缩和解压缩文件工具类
- Java操作zip压缩和解压缩文件工具类
- Java压缩文件和解压缩文件
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- java 压缩文件
- java压缩文件
- qt在linux与ok6410上的安装移植(5)——tslib和qt在开发板上的移植
- nextInt与nextline()混合使用
- Resharper上手指南
- Java高级部分笔记
- 今生得到你是我最大的幸福,如果有來生,我還希望能娶妳
- java压缩文件工具类
- 找实习经历——盘点各大公司的拒信
- HDU 1890 Robotic Sort 单点查找区间翻转 Splay裸题
- Can't create handler inside thread that has not called Looper.prepare()解决办法
- 教你走出“事情多”的忙乱困局
- map
- HDU 1847 Good Luck in CET-4 Everybody!
- libgdx编译的时候报错
- 关于“warning C4786”