博客
关于我
Java实现压缩与解压
阅读量:284 次
发布时间:2019-03-01

本文共 3443 字,大约阅读时间需要 11 分钟。

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
 * 压缩与解压  
 * @author hulujie
 *
 */
public class ZipUtil {
    
    /**
     * 压缩文件
     * 
     * @param srcfile
     *            File[] 需要压缩的文件列表  "F:\\asd"
     * @param zipfile
     *            File 压缩后的文件 "f:\\qwe_16_V6.0.0_1.zip";
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) throws IOException  {
        
        
        
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipfile));
            for(int i = 0; i < srcfile.length; ++i){
                if(srcfile[i].isDirectory()){
                    zipDir(zipOut,srcfile[i],srcfile[i].getName());
                }else{
                    zipFile(zipOut,srcfile[i],null);
                }
            }
        zipOut.close();
    }
    /**
     * 递归压缩文件夹 faterName的目的就是为了维持原来的目录
     * @author hulujie
     * @since 2016年9月2日 下午5:33:26
     * @param zipOut
     * @param dirFile
     * @param faterName
     * @return
     * @throws IOException
     */
    private static ZipOutputStream zipDir(ZipOutputStream zipOut,File dirFile,String faterName) throws IOException{
        File fileList[] = dirFile.listFiles();
        
        for (int i = 0; i < fileList.length; i++) {
            if(fileList[i].isDirectory()){
                zipDir(zipOut,fileList[i],faterName+File.separator+fileList[i].getName());
            }else{
                zipFile(zipOut,fileList[i],faterName);
            }
        }
        return zipOut;
    }
    
    /**
     * 压缩单个文件
     * @author hulujie
     * @since 2016年9月2日 下午5:40:26
     * @param zipOut
     * @param file
     * @throws IOException
     */
    private static void zipFile(ZipOutputStream zipOut,File file,String faterName) throws IOException{
        
        InputStream input = null;
            
        byte[] buf = new byte[1024];
        input = new FileInputStream(file);
        if(faterName!=null){
             zipOut.putNextEntry(new ZipEntry(faterName+File.separator+file.getName()));
        }else{
            zipOut.putNextEntry(new ZipEntry(File.separator+file.getName()));
        }
       
        int temp = 0;
        while((temp = input.read(buf)) != -1){
            zipOut.write(buf,0,temp);
        }
        input.close();
    }
    
    
    /**
     * zip解压缩
     * 
     * @param zipfile
     *            File 需要解压缩的文件 "f:\\qwe_16_V6.0.0_1.zip";
     * @param descDir
     *            String 解压后的目标目录 "F:\\asd";
     */
    public  static void unFiles(File file, String outputDirectory) throws IOException {
        
        File outzipFile = new File(outputDirectory);
        if(!outzipFile.exists()){
            outzipFile.mkdirs();
        }
        byte[] buf = new  byte[1024];
        
        File outFile = null;
        ZipFile zipFile = new ZipFile(file);
        ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
        ZipEntry entry = null;
        InputStream input = null;
        OutputStream output = null;
        while((entry = zipInput.getNextEntry()) != null){
            outFile = new File(outputDirectory + File.separator + entry.getName());
            
            if(entry.isDirectory()){
                outFile.mkdir();
            }else{
                
                if(!outFile.getParentFile().exists()){
                    outFile.getParentFile().mkdirs();
                }
                
                outFile.createNewFile();
                 input = zipFile.getInputStream(entry);
                 output = new FileOutputStream(outFile);
                 int temp = 0;
                 while((temp = input.read(buf)) != -1){
                     output.write(buf,0,temp);
                 }
                 input.close();
                 output.close();
            }
        }
        zipFile.close();
        zipInput.close();
    }
    
    public static void main(String[] args){
        
        String dirPath = "F:\\asd";
        String zipFilePath = "f:\\qwe_16_V6.0.0_1.zip";
        
        try {
            File file = new File(dirPath);
            File zipFile = new File(zipFilePath);
            if(!zipFile.exists()){
                zipFile.createNewFile();
            }
            
            ZipFiles(file.listFiles(),zipFile);
            
//            unFiles(zipFile,dirPath);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
    }
}
 

转载地址:http://txgo.baihongyu.com/

你可能感兴趣的文章
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
mysql order by多个字段排序
查看>>