Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 411|回复: 0

[Java基础知识]压缩文件和目录

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-9-30 21:46:25 | 显示全部楼层 |阅读模式
    import java.io.*;
    import java.util.zip.*;

    /**
      * 这个类定义了两种静态方法,一个使用gzip压缩文件
      * 一个使用zip压缩目录。
      **/
    public class Compress {
         /** 用Gzip压缩文件的内容并存入文件 */
         public static void gzipFile(String from, String to)
          throws IOException {
             // Create stream to read from the from file
             FileInputStream in = new FileInputStream(from);
             // 创建GZIP输出流来压缩数据并写入to文件
             GZIPOutputStream out =
                  new GZIPOutputStream(new FileOutputStream(to));
             // 从一个流复制数据到另一个流
             byte[] buffer = new byte[4096];
             int bytes_read;
             while((bytes_read = in.read(buffer)) != -1)  
                 out.write(buffer, 0, bytes_read);
             // And close the streams
             in.close();
             out.close();
         }  
      
       
       
         
       

       
       
      
         /** 用Zip压缩目录的内容并存入到zip文件 */
         public static void zipDirectory(String dir, String zipfile)throws IOException, IllegalArgumentException {
             // 检测是否是目录,获取其内容
             File d = new File(dir);
             if (!d.isDirectory())
                 throw new IllegalArgumentException("Compress: not a directory:  " +dir);
             String[] entries = d.list();//获取dir目录下所有目录和文件
             byte[] buffer = new byte[4096];  // 创建复制缓冲区  
             int bytes_read;

             //创建一个zip输出流来压缩数据并写入到zip文件
             ZipOutputStream out =new ZipOutputStream(new FileOutputStream(zipfile));

             // 遍历目录所有项
             for(int i = 0; i < entries.length; i++) {
                 File f = new File(d, entries);
                 if (f.isDirectory()) continue;        // 不处理子目录
                 FileInputStream in = new FileInputStream(f); // 创建一个文件输入流
                 ZipEntry entry = new ZipEntry(f.getPath());  // 做一个ZipEntry
                 out.putNextEntry(entry);                     // 存储项信息到压缩文件
                 while((bytes_read = in.read(buffer)) != -1)  // 复制字节到压缩文件
                     out.write(buffer, 0, bytes_read);
                 in.close();                                  // Close input stream
             }
             // When we"re done with the whole loop, close the output stream
             out.close();
         }

         /**
          * 测试代码
          **/
         public static class Test {
             /**
      * 压缩一个指定的文件或目录.  If no destination name is
      * specified, append .gz to a file name or .zip to a directory name
      **/
             public static void main(String args[]) throws IOException {
                 if ((args.length != 1)&& (args.length != 2)) {  // check arguments
                     System.err.println("Usage: java Compress$Test <from> [<to>]");
                     System.exit(0);
                 }
                 String from = args[0], to;
                 File f = new File(from);
                 boolean directory = f.isDirectory();  // 是文件还是目录?
                 if (args.length == 2) to = args[1];     
                 else {                             
                     if (directory) to = from + ".zip";   //   如果是目录,用.zip后缀
                     else to = from + ".gz";              //   如果是文件,用.gz后缀
                 }

                 if ((new File(to)).exists()) { // 如果目的文件已存在,不覆盖它,退出.
                  System.err.println("Compress: won"t overwrite existing file: "+to);
                     System.exit(0);
                 }

                 // 最后,调用上面方法之一压缩
                 if (directory) Compress.zipDirectory(from, to);
                 else Compress.gzipFile(from, to);
             }
         }
    }


    运行:

    C:java>java   Compress$Test  Compress.java 1.zip

    C:java>
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-10 00:32 , Processed in 0.442283 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表