• java文件压缩和解压


    功能实现.

    package com.test;
    
    import java.io.File;
    import java.io.BufferedOutputStream;
    import java.io.BufferedInputStream;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    
    /**
     * 该类实现文件夹压缩成zip文件和zip文件解压
     * 
     * @author Administrator
     * 
     */
    public class Zip {
        private ZipInputStream zipIn; // 解压Zip
        private ZipOutputStream zipOut; // 压缩Zip
        private ZipEntry zipEntry;
        private static int bufSize; // size of bytes
        private byte[] buf;
        private int readedBytes;
    
        public Zip() {
            this(512);
        }
    
        public Zip(int bufSize) {
            this.bufSize = bufSize;
            this.buf = new byte[this.bufSize];
        }
    
        // 压缩文件夹内的文件
        public void doZip(String zipDirectory) {// zipDirectoryPath:需要压缩的文件夹名
            File file;
            File zipDir;
    
            zipDir = new File(zipDirectory);
            String zipFileName = zipDirectory + ".zip";// 压缩后生成的zip文件名
    
            try {
                this.zipOut = new ZipOutputStream(new BufferedOutputStream(
                        new FileOutputStream(zipFileName)));
                handleDir(zipDir, this.zipOut);
                this.zipOut.close();
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
    
        // 由doZip调用,递归完成目录文件读取
        private void handleDir(File dir, ZipOutputStream zipOut) throws Exception {
            FileInputStream fileIn;
            File[] files;
    
            files = dir.listFiles();
            if (files.length == 0) {// 如果目录为空,则单独创建之.
                // ZipEntry的isDirectory()方法中,目录以"/"结尾.
                this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
                this.zipOut.closeEntry();
            } else {// 如果目录不为空,则分别处理目录和文件.
                for (File fileName : files) {
    
                    if (fileName.isDirectory()) {
                        handleDir(fileName, this.zipOut);
                    } else {
                        fileIn = new FileInputStream(fileName);
                        String name = dir.getName();
                        // 生成的压缩包存放在原目录下
                        this.zipOut.putNextEntry(new ZipEntry(name + "/"
                                + fileName.getName().toString()));
    
                        // 此方法存放在该项目目录下
                        // this.zipOut.putNextEntry(new
                        // ZipEntry(fileName.toString()));
                        while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
                            this.zipOut.write(this.buf, 0, this.readedBytes);
                        }
                        this.zipOut.closeEntry();
                    }
                }
            }
        }
    
        // 解压指定zip文件
        public void unZip(String unZipfileName) {// unZipfileName需要解压的zip文件名
            FileOutputStream fileOut;
            File file;
            String f = unZipfileName.substring(0, unZipfileName.length() - 4);
            File ff = new File(f);
            try {
                this.zipIn = new ZipInputStream(new BufferedInputStream(
                        new FileInputStream(unZipfileName)));
                while ((this.zipEntry = this.zipIn.getNextEntry()) != null) {
                    file = new File(this.zipEntry.getName());
                    if (this.zipEntry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        // 如果指定文件的目录不存在,则创建之.
                        File parent = file.getParentFile();
                        if (!parent.exists()) {
                            parent.mkdirs();
                        }
                        if (!ff.exists()) {
                            ff.mkdir();
                        }
                        fileOut = new FileOutputStream(f + "/" + file.getName());
    
                        // fileOut = new FileOutputStream(file); 此方法存放到该项目目录下
                        while ((this.readedBytes = this.zipIn.read(this.buf)) > 0) {
                            fileOut.write(this.buf, 0, this.readedBytes);
                        }
                        fileOut.close();
                    }
    
                    this.zipIn.closeEntry();
                }
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }
        }
    
        // 设置缓冲区大小
        public void setBufSize(int bufSize) {
            this.bufSize = bufSize;
        }
    
        // 测试Zip类
        public static void main(String[] args) throws Exception {
            Zip zip = new Zip();
            zip.doZip("e:\test");
    //        zip.unZip("c:\test.zip");
        }
    }
    你配不上自己的野心 也辜负了所受的苦难
  • 相关阅读:
    jQuery中使用了document和window哪些属性和方法
    jQuery.extend函数解析
    我的音乐播放器样式初稿
    jQuery插件xmlDOM源码学习
    从jQuery.camelCase()学习string.replace()
    document.getElementById到底是什么东西?
    LESS和Sass异同
    【转】查找应用中的Private API
    (转)SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 shiney
    SQL的跨服务器查询(表,视图一样) shiney
  • 原文地址:https://www.cnblogs.com/wdnnccey/p/5854396.html
Copyright © 2020-2023  润新知