|
|
大 小
楼主
Java压缩文件和文件夹代码示例(生成zip文件) Post by : 2010-09-17 18:21:17.0
|

- 幼儿园
- UID:2
- 主题:126
- 帖子:219
-
加为好友
发送短信
|
java代码
- package test;
-
- 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;
-
- public class Zip {
-
-
-
-
-
-
-
- public void zipFolder(String zipPath, String filePath) throws Exception {
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
- File f = new File(filePath);
- zipFiles(out, f, "");
- out.close();
- }
-
-
-
-
-
-
-
- public void zipFile(String zipPath, String filePath) throws Exception {
- File f = new File(filePath);
- FileInputStream fis = new FileInputStream(f);
- BufferedInputStream bis = new BufferedInputStream(fis);
- byte[] buf = new byte[1024];
- int len;
- FileOutputStream fos = new FileOutputStream(zipPath);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- ZipOutputStream zos = new ZipOutputStream(bos);
- ZipEntry ze = new ZipEntry(f.getName());
- zos.putNextEntry(ze);
-
- while ((len = bis.read(buf)) != -1) {
- zos.write(buf, 0, len);
- zos.flush();
- }
- bis.close();
- zos.close();
-
- }
-
-
-
-
-
-
-
-
- private void zipFiles(ZipOutputStream out, File f, String base) throws Exception {
- if (f.isDirectory()) {
- File[] fl = f.listFiles();
- out.putNextEntry(new ZipEntry(base + "/"));
- base = base.length() == 0 ? "" : base + "/";
- for (int i = 0; i < fl.length; i++) {
- zipFiles(out, fl[i], base + fl[i].getName());
- }
- } else {
- out.putNextEntry(new ZipEntry(base));
- FileInputStream in = new FileInputStream(f);
- int b;
-
- while ((b = in.read()) != -1) {
- out.write(b);
- }
- in.close();
- }
- }
-
-
-
-
- public static void main(String[] args) {
- System.out.println("Zip File Begin");
-
- Zip zip = new Zip();
- String zipPath = "E:\\test.zip";
- String filePath = "E:\\index.pdf";
- try {
- zip.zipFile(zipPath, filePath);
-
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- System.out.println("Zip File Done");
- }
-
- }
|
|
|
|