压缩包实例

 package zip;
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.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class TestZip {

public static void main(String[] args) throws Exception{
  //将所有目录及其子目录中的文件压缩到一个目录下
  zip("E:/test/123.zip","E:/test/qq");
  //原样压缩到指定的zip包名中
//  zip1("E:/test/123.zip","E:/test/qq");
  
  //得到压缩包中包有名子
//  readZip("E:/test/shuju.zip");
  //解压缩包
//  unZip("E:/test/shuju.zip");
  //解压缩包
//  unZip1("E:/test/shuju.zip");
  
}

/**
  * 将目录里的所有文件及其子目录中的文件都压缩到一个给定zip名的压缩包的根目录中
  * @param fileName   压缩后的文件名
  * @param directory  要压缩的文件或文件目录
  * @throws Exception
  */
public static void zip(String fileName,String directory) throws Exception{
  
  FileOutputStream dest=new FileOutputStream(fileName);
  ZipOutputStream out=new ZipOutputStream(new BufferedOutputStream(dest));  
  
  File f=new File(directory);
  //如果参数是目录
  if(f.isDirectory()){
   tt(f,out);
  }else{
   ZipEntry entry=new ZipEntry(f.getName());
   out.putNextEntry(entry);
  
   //写入文件内容
   FileInputStream in=new FileInputStream(f);
   byte[] data=new byte[1024];
   int count;
   while((count=in.read(data))!=-1){
    out.write(data,0,count);
   }
   //刷新zip输出流
   out.flush();
   in.close();
  }
  out.close();
  
}

/**
  * 递归方法
  * @param f
  * @param out
  * @throws Exception
  */
public static void tt(File f,ZipOutputStream out) throws Exception{
  BufferedInputStream bin=null;
  byte[] data=new byte[1024];
  
  File[] files=f.listFiles();
  for(int i=0;i<files.length;i++){
   File file=files[i];
   if(file.isFile()){
    //使用文件名创建新的文件条目
    ZipEntry entry=new ZipEntry(file.getName());
    //开始写入新的文件条目并将流定位到条目数据的开始处
    out.putNextEntry(entry);
    
    //写入文件内容
    bin=new BufferedInputStream(new FileInputStream(files[i]));    
    int count;
    while((count=bin.read(data))!=-1){
     out.write(data,0,count);
    }
    bin.close();
    //刷新zip输出流
    out.flush();
    
   }else{
    //递归调用,自己调用自己
    tt(file,out);
   }
  }
  
}
/**
  * 压缩zip文件,将传过来的目录结构原样压缩
  * @param fileName   压缩后的文件名
  * @param directory  要压缩的文件或文件目录
  * @throws Exception
  * @throws Exception
  */
public static void zip1(String fileName,String directory) throws Exception{  
  FileOutputStream dest=new FileOutputStream(fileName);
  ZipOutputStream out=new ZipOutputStream(new BufferedOutputStream(dest));  
  
  File f=new File(directory);
  if(f!=null&&f.exists()){
   //如果参数是目录
   if(f.isDirectory()){
    String path=f.getName()+"/";
    t(f,out,path);
   }else{
    ZipEntry entry=new ZipEntry(f.getName());
    out.putNextEntry(entry);
    
    //写入文件内容
    FileInputStream in=new FileInputStream(f);
    byte[] data=new byte[1024];
    int count;
    while((count=in.read(data))!=-1){
     out.write(data,0,count);
    }
    //刷新zip输出流
    out.flush();
    in.close();
   }
  }else
   System.out.println("文件不存在");
  out.close();
}
/**
  * 通过递归 遍历文件夹及其子目录
  * @param f    要压缩的目录
  * @param out   zip输出流
  * @param p    文件的上级目录
  * @throws Exception
  */
public static void t(File f,ZipOutputStream out,String p) throws Exception{
  File[] files=f.listFiles();
  int i=0;
  for(i=0;i<files.length;i++){
   File file=files[i];
   if(file.isFile()){
    String path=p+file.getName();
    System.out.println("文件名:"+path);    
    ZipEntry entry=new ZipEntry(path);
    out.putNextEntry(entry);
    //写入文件内容
    FileInputStream in=new FileInputStream(files[i]);
    byte[] data=new byte[1024];
    int count;
    while((count=in.read(data))!=-1){
     out.write(data,0,count);
    }
    //刷新zip输出流
    out.flush();
    in.close();
    
   }else{
    String path=p+file.getName()+"/";
    //递归调用,自己调用自己
    t(file,out,path);
   }
  }
  
  //如果i==0,说明此文件夹是空的,上面的循环,只能将不为的文件夹的内容压缩进去,所以在这个地方自己要添加
  if(i==0){
   ZipEntry entry1=new ZipEntry(p);
   out.putNextEntry(entry1);  
  }
}

/**
  * 打印出压缩包中所有文件及文件夹的名子
  */
public static void readZip(String fileOrName) throws IOException{
  ZipFile zipFile=new ZipFile(fileOrName);//需捕获异常
  //返回 ZIP 文件的路径名。
  String zipName=zipFile.getName();
  //返回 ZIP 文件中的条目数
  int size=zipFile.size();
  System.out.println("文件"+zipName+"下共有条目"+size+"个");
  
  //返回 ZIP 文件条目的枚举。包括所有文件夹、文件、子文件夹和子文件夹中的东西
  Enumeration emu=zipFile.entries();
  
  System.out.println("文件夹有:");
  while(emu.hasMoreElements()){
   ZipEntry entry=(ZipEntry)emu.nextElement();
  
   if(entry.isDirectory()){
    //返回条目名称。
    String entryName=entry.getName();
    System.out.println(entryName);
   }
  }
  Enumeration emu1=zipFile.entries();
  System.out.println("文件有:");
  while(emu1.hasMoreElements()){
   ZipEntry entry=(ZipEntry)emu1.nextElement();  
   if(!entry.isDirectory()){
    //返回条目名称。
    String entryName=entry.getName();
    System.out.println(entryName);
   }
  }
  zipFile.close();
}

/**
  * 将压缩包中的内容解压出来
  * @param fileOrName
  * @throws IOException
  */
public static void unZip(String fileOrName) throws IOException{  
  File file=new File(fileOrName);
  ZipFile zipFile=new ZipFile(file);//需捕获异常
  Enumeration emu=zipFile.entries();
  while(emu.hasMoreElements()){
   ZipEntry entry=(ZipEntry)emu.nextElement();
            if (!entry.isDirectory())
            {
             String fileName=entry.getName();
            
             System.out.println(file.getParent()+"/" + fileName);
            
             File file1=new File(file.getParent()+"/"+fileName);
             File parent=file1.getParentFile();
             //如果文件的父目录不存在,则创建
             if(parent != null && (!parent.exists())){
                    parent.mkdirs();
                }            
             //返回输入流以读取指定 ZIP 文件条目的内容。
                BufferedInputStream bin = new BufferedInputStream(zipFile.getInputStream(entry));
                
                FileOutputStream out=new FileOutputStream(file1);
                //创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。
                BufferedOutputStream bout=new BufferedOutputStream(out);
                
                byte[] data=new byte[1024];
                int i=0;
                while((i=bin.read(data))!=-1){
                 bout.write(data, 0, i);
                }
                bout.flush();
                bin.close();
                bout.close();                
            }
  }
  zipFile.close();
}

/**
  * 将压缩包中的内容解压出来
  * @param fileOrName
  * @throws IOException
  */
public static void unZip1(String fileOrName) throws IOException{  
  File file=new File(fileOrName);
  ZipFile zipFile=new ZipFile(file);//需捕获异常
  Enumeration emu=zipFile.entries();
  while(emu.hasMoreElements()){
   ZipEntry entry=(ZipEntry)emu.nextElement();
   //会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。
            if (entry.isDirectory())
            {
                new File(file.getParent()+"/" + entry.getName()).mkdirs();
                continue;
            }
            //返回输入流以读取指定 ZIP 文件条目的内容。
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file1=new File(file.getParent()+"/" +entry.getName());
            //加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件
            //而这个文件所在的目录还没有出现过,所以要建出目录来。
            
            //返回此抽象路径名的父路径名的抽象路径名,如果此路径名没有指定父目录,则返回 null。
            File parent=file1.getParentFile();
            if(parent != null && (!parent.exists())){
                parent.mkdirs();
            }
            System.out.println(file1.getPath());
            
            FileOutputStream fos=new FileOutputStream(file1);
            //创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。
            BufferedOutputStream bos=new BufferedOutputStream(fos,2048);
            
            int count;
            byte[] data=new byte[2024];
            while((count=bis.read(data,0,2024))!=-1){
             bos.write(data,0,count);
            }
            bos.flush();
            bos.close();
            bis.close();
  }
  zipFile.close();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值