zoukankan      html  css  js  c++  java
  • Java—将文件夹压缩为zip文件

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipOutputStream;
    /**
     * 
     * @author hwt
     *
     */
    public class TestDir {
        /**
         * 将文件夹以及其下的文件压缩为文件
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
    //源文件夹 File file
    = new File("D:/ziptest");
         //目的文件 ZipOutputStream zos
    = new ZipOutputStream(new FileOutputStream("D:/test.zip")); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i=0; i<files.length; i++) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i])); zos.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName())); while (true) { byte[] b = new byte[100]; int len = bis.read(b); if (len == -1) break; zos.write(b, 0, len); } bis.close(); } } zos.close(); } }
  • 相关阅读:
    数据仓库010
    R语言- 实验报告
    数据仓库006
    数据仓库009
    多台Linux 7.x服务器具有相同的UUID网络链接参数,肿么办?
    数据仓库005
    数据仓库004
    我的编程竞赛生涯
    我的建模竞赛生涯
    再见了,亲爱的博客园
  • 原文地址:https://www.cnblogs.com/smart-hwt/p/8275466.html
Copyright © 2011-2022 走看看