zoukankan      html  css  js  c++  java
  • 文件压缩

    package com.jcsoft.transaction.common.action;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    /**
     * 压缩文件
     */
    public class DownPackAction {
         public static File doZip(String sourceDir, String zipFilePath) 
                 throws IOException {
                  
                  File file = new File(sourceDir);
                  File zipFile = new File(zipFilePath);
                  ZipOutputStream zos = null;
                  try {
                   // 创建写出流操作
                   OutputStream os = new FileOutputStream(zipFile);
                   BufferedOutputStream bos = new BufferedOutputStream(os);
                   zos = new ZipOutputStream(bos);
                   
                   String basePath = null;
                   
                   // 获取目录
                   if(file.isDirectory()) {
                    basePath = file.getPath();
                   }else {
                    basePath = file.getParent();
                   }
                   
                   zipFile(file, basePath, zos);
                  }finally {
                   if(zos != null) {
                    zos.closeEntry();
                    zos.close();
                   }
                  }
                  
                  return zipFile;
                 }
    
                 /** 
                  * @param source 源文件
                  * @param basePath 
                  * @param zos 
                  */
                 private static void zipFile(File source, String basePath, ZipOutputStream zos) 
                 throws IOException {
                  File[] files = null;
                  if (source.isDirectory()) {
                   files = source.listFiles();
                  } else {
                   files = new File[1];
                   files[0] = source;
                  }
                  
                  InputStream is = null;
                  String pathName;
                  byte[] buf = new byte[1024];
                  int length = 0;
                  try{
                   for(File file : files) {
                    if(file.isDirectory()) {
                     pathName = file.getPath().substring(basePath.length() + 1) + "/";
                     zos.putNextEntry(new ZipEntry(pathName));
                     zipFile(file, basePath, zos);
                    }else {
                     pathName = file.getPath().substring(basePath.length() + 1);
                     is = new FileInputStream(file);
                     BufferedInputStream bis = new BufferedInputStream(is);
                     zos.putNextEntry(new ZipEntry(pathName));
                     while ((length = bis.read(buf)) > 0) {
                      zos.write(buf, 0, length);
                     }
                    }
                   }
                  }finally {
                   if(is != null) {
                    is.close();
                   }
                  }
                 }
                 public static void main(String[] args) {   
                     try {
                         //将D:\test1文件夹打包到D:\test2并命名为test1.zip
                        doZip("D:\test1","D:\test2\test1.zip"); 
                    } catch (IOException e) {
                        e.printStackTrace();
                    }   
                    }   
    }
  • 相关阅读:
    CSS 单行超出隐藏
    python开发学习day12 (函数参数;函数对象)
    作业day11
    python开发学习day11 (函数; 返回值 ; 形参与实参 )
    python开发学习day10 (文件指针; 文本修改)
    作业day09
    python开发学习day09 (with上下文处理; b模式; +模式; 文件操作)
    作业day08
    python开发学习day08 (集合; 文件操作)
    作业day07
  • 原文地址:https://www.cnblogs.com/angto64/p/6177957.html
Copyright © 2011-2022 走看看