Java建立Zip压缩文件的代码实例
Java代码
  1. import java.io.*;
  2. import java.util.zip.*;
  3. class Zip {
  4. public static void main(String args[]) throws IOException {
  5. byte b[] = new byte[512];
  6. ZipOutputStream zout = new ZipOutputStream(System.out);
  7. for(int i = 0; i < args.length; i ++) {
  8. InputStream in = new FileInputStream(args[i]);
  9. ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));
  10. zout.putNextEntry(e);
  11. int len=0;
  12. while((len=in.read(b)) != -1) {
  13. zout.write(b,0,len);
  14. }
  15. zout.closeEntry();
  16. print(e);
  17. }
  18. zout.close();
  19. }
  20. public static void print(ZipEntry e){
  21. PrintStream err = System.err;
  22. err.print("added " + e.getName());
  23. if (e.getMethod() == ZipEntry.DEFLATED) {
  24. long size = e.getSize();
  25. if (size > 0) {
  26. long csize = e.getCompressedSize();
  27. long ratio = ((size-csize)*100) / size;
  28. err.println(" (deflated " + ratio + "%)");
  29. }
  30. else {
  31. err.println(" (deflated 0%)");
  32. }
  33. }
  34. else {
  35. err.println(" (stored 0%)");
  36. }
  37. }
  38. }

压缩文件代码:
Java代码
  1. import java.io.*;
  2. import java.util.zip.*;//ZipOutputStream这个流在这里
  3. import java.util.*;
  4. public class ZipOutputStreamTest
  5. {
  6. public static void main(String[] args) throws Exception
  7. {
  8. File f = new File("a.jpg");
  9. FileInputStream fis = new FileInputStream(f);
  10. BufferedInputStream bis = new BufferedInputStream(fis);
  11. byte[] buf = new byte[1024];
  12. int len;
  13. FileOutputStream fos = new FileOutputStream(f.getName()+".zip");
  14. BufferedOutputStream bos = new BufferedOutputStream(fos);
  15. ZipOutputStream zos = new ZipOutputStream(bos);//压缩包
  16. ZipEntry ze = new ZipEntry(f.getName());//这是压缩包名里的文件名
  17. zos.putNextEntry(ze);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处
  18. while((len=bis.read(buf))!=-1)
  19. {
  20. zos.write(buf,0,len);
  21. zos.flush();
  22. }
  23. bis.close();
  24. zos.close();
  25. }
  26. }

压缩文件夹代码:
Java代码
  1. import java.io.File;
  2. import org.apache.tools.zip.ZipOutputStream; //这个包在ant.jar里,要到官方网下载
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.util.zip.ZipInputStream;
  6. import java.util.zip.ZipEntry;
  7. public class CompressBook {
  8. public CompressBook() {}
  9. /**//*
  10. * inputFileName 输入一个文件夹
  11. * zipFileName 输出一个压缩文件夹
  12. */
  13. public void zip(String inputFileName) throws Exception {
  14. String zipFileName = "c:\\test.zip"; //打包后文件名字
  15. System.out.println(zipFileName);
  16. zip(zipFileName, new File(inputFileName));
  17. }
  18. private void zip(String zipFileName, File inputFile) throws Exception {
  19. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
  20. zip(out, inputFile, "");
  21. System.out.println("zip done");
  22. out.close();
  23. }
  24. private void zip(ZipOutputStream out, File f, String base) throws Exception {
  25. if (f.isDirectory()) {
  26. File[] fl = f.listFiles();
  27. out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
  28. base = base.length() == 0 ? "" : base + "/";
  29. for (int i = 0; i < fl.length; i++) {
  30. zip(out, fl[i], base + fl[i].getName());
  31. }
  32. }else {
  33. out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
  34. FileInputStream in = new FileInputStream(f);
  35. int b;
  36. System.out.println(base);
  37. while ( (b = in.read()) != -1) {
  38. out.write(b);
  39. }
  40. in.close();
  41. }
  42. }
  43. public static void main(String [] temp){
  44. CompressBook book = new CompressBook();
  45. try {
  46. book.zip("c:\\c");//你要压缩的文件夹
  47. }catch (Exception ex) {
  48. ex.printStackTrace();
  49. }
  50. }
  51. }