Java压缩文件和文件夹

好久没有更新了……………………
此处省略一万字。胡汉三又回来啦!!!!言归正传。。。

本次主要是实现了功能:根据传入的路径,压缩文件和文件夹,例如从A路径下的文件压缩到B目录,其中包括空文件夹。废话不多说,直接上代码:(这是一个工具类,idea亲测可用!!)

                        import java.util.zip.ZipFile;
                        import  org.apache.tools.zip.*;
                        import java.io.*;

                    /*********  1.压缩文件和文件夹   *******/   
       public  static void zipFile(ZipOutputStream zipOutputStream, File file, String parentFileName) {
       FileInputStream in = null;
        try {
        org.apache.tools.zip.ZipEntry zipEntry = new org.apache.tools.zip.ZipEntry(parentFileName);
        //  ZipEntry zipEntry = new ZipEntry(file.getName());
        zipEntry.setTime(file.lastModified());
        zipOutputStream.putNextEntry(zipEntry);
        in = new FileInputStream(file);
        int len;
        byte[] buf = new byte[8 * 1024];
        while ((len = in.read(buf)) != -1) {
            zipOutputStream.write(buf, 0, len);
        }
        zipOutputStream.closeEntry();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

    /**
 * 2.递归压缩目录结构
 *
 * @param zipOutputStream
 * @param file
 * @param parentFileName
 */
public  static void directory(org.apache.tools.zip.ZipOutputStream zipOutputStream, File file, String parentFileName) {
    File[] files = file.listFiles();
    String parentFileNameTemp = null;
    for (File fileTemp : files) {
        String   fileTempName = fileTemp.getName().substring(fileTemp.getName().indexOf(".") + 1);
        if(fileTempName.equals("zip")){
            fileTemp.delete();
            continue;
        }
        parentFileNameTemp = com.dy.core.sims.utils.StringUtils.isEmpty(parentFileName) ? fileTemp.getName() : parentFileName + "/" + fileTemp.getName();
        if (fileTemp.isDirectory()) {
            directory(zipOutputStream, fileTemp, parentFileNameTemp);
        }else {
            zipFile(zipOutputStream, fileTemp, parentFileNameTemp);
        }
    }

    if(files.length==0){
        try{
            zipOutputStream.putNextEntry(new ZipEntry (getPath(file)+File.separator));
        }catch (Exception e){
            e.printStackTrace();
            }

             }
          }

           //3.获得该文件在压缩包中的相对路径
      public  static  String getPath (File f) {
    String str1 = f.getAbsolutePath();
    int n1= str1.length();
    String str2 = f.getAbsolutePath();
    int n2= str2.length();
    String str3 = f.getName();
    int n3= str3.length();
    String str = str2.substring(n1-n3, n2);
    return str;
       }

                 /**
 * 4.压缩文件目录
 *
 * @param source 源文件目录(单个文件和多层目录)
 * @param destit 目标目录
 */
public static Boolean  compressFiles(String source, String destit) {
    //判断路径是否存在
    String   de=destit.substring(0, destit.lastIndexOf("//")+2);
    File  targetFile=new File(source);
    File  fileTarget=new File(de);
    if(!targetFile.exists() || !fileTarget.exists()){
        return false;
    }
    File file = new File(source);
    org.apache.tools.zip.ZipOutputStream zipOutputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(destit);
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        if (file.isDirectory()) {
            directory(zipOutputStream, file, "");
        } else {
            zipFile(zipOutputStream, file, "");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            zipOutputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return  true;
      }

                /**
 * 测试
 *
 * @param args
 */
public static void main(String[] args) {

    try{

       Boolean  flag=compressFiles("D://Excel//","D://Excel//aa.zip");
        System.out.println(flag);
    }catch (Exception e){
        e.printStackTrace();
         }
    }

全部代码就在这上面,需要用的jar包是org.apache.tools.zip,各位有需要的可参考,如有不正确,希望各位大神不惜赐教

猜你喜欢

转载自blog.51cto.com/12237592/2362934
0条评论
添加一条新回复