`

压缩与解压缩文档

阅读更多
package com.sjs;

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.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class UtilZip {

    public static void main(String[] args) throws Exception {

        String type = args[0];// 文件压缩类型

        // 如果命令行压缩,目前只支持压缩单个档案
        if ("zip" == type) {

            String src = args[1];// 源文件
            String[] srcFile = { src };
            String savePath = args[2];// 目标文件目录
            String destFile = args[3];// 目标文件名称
            // // 调用压缩文件方法
            zip(srcFile, savePath, destFile);
        } else if ("unzip" == type) {

            String savePath = args[1];// 解压文件之后所放置的目录
            String destFile = args[2];// 源压缩文件
            // 调用压缩文件方法
            unZip(destFile, savePath);
        } else {
            throw new RuntimeException(" $1 must be zip or unzip .");
        }

    }

    /**
     * 
     * @param source
     *         源文件
     * @param savePath
     *         压缩文件路径
     * @param destFileName
     *         压缩文件名
     * @throws Exception
     * 
     * @Description 压缩文件方法
     */
    public static void zip(String[] source, String savePath, String destFileName) throws Exception {

        // 检查被压缩元素是否存在,并创建目标目录
        init(source, savePath, destFileName);

        File destFile = new File(savePath, destFileName);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            // 打开目标文件
            fos = new FileOutputStream(destFile);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("open" + destFile.getAbsolutePath() + "operate fail cause " + e);
        }

        zos = new ZipOutputStream(fos);
        try {
            for (int i = 0; i < source.length; i++) {
                File srcFile = new File(source[i]);

                // 将盘符去掉:由“c:/home”变成“home”;即把绝对路径换成相对路径进行压缩
                String entryPath = source[i].substring(source[i].indexOf("/") + 1, source[i].length());
                if (srcFile.isDirectory()) {
                    String parent = srcFile.getParent();
                    System.out.println("parent :" + parent);
                    // 压缩目录档案
                    zip(zos, srcFile, parent);
                }
                // 压缩非目录档案
                if (!(srcFile.isDirectory())) {
                    fis = new FileInputStream(new File(source[i]));
                    ZipEntry entry = new ZipEntry(entryPath);
                    zos.putNextEntry(entry);
                    writeFile(fis, zos);
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 
     * @param zipOutputStream
     *         压缩文件输出流
     * @param file
     *         被压缩文件
     * @param src
     *         被压缩文件的父目录
     */
    private static void zip(ZipOutputStream zipOutputStream, File file, String src) {
        if (file.isDirectory()) {
            File[] fileList = file.listFiles();
            String entryPath = file.getAbsolutePath().substring(src.length()) + "/";
            try {
                // ##### 对目录进行处理,如果没有此句,则空目录和非空目录都不会被压缩进去
                zipOutputStream.putNextEntry(new ZipEntry(entryPath));
            } catch (Exception e) {
                e.printStackTrace();
            }

            for (int i = 0; i < fileList.length; ++i)
                zip(zipOutputStream, fileList[i], src);
        } else {
            FileInputStream in = null;
            try {
                String fileName = file.getAbsolutePath();
                in = new FileInputStream(file);
                String entryPath = fileName.substring(src.length());
                // 将此语句解注,配合将“#####”语句注释掉,可以将所有目录去掉,只压缩文件,不会由目录出现(即压缩文件中不会由目录结构)
                // entryPath = entryPath.substring(entryPath.lastIndexOf("\\") +
                // 1);
                ZipEntry entry = new ZipEntry(entryPath);
                zipOutputStream.putNextEntry(entry);
                writeFile(in, zipOutputStream);
            } catch (Exception e) {
                throw new RuntimeException("zip file " + file.getAbsolutePath() + " has failed.");
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
        }
    }

    // 将数据写入输出流
    private static void writeFile(InputStream fis, OutputStream zos) throws IOException {
        int length = 0;
        byte[] buffer = new byte[1024];
        while ((length = fis.read(buffer)) != -1) {
            zos.write(buffer, 0, length);
        }
        zos.flush();
    }

    /**
     * 
     * @param source
     * @param savePath
     * @param destFileName
     * @Description: 初始化压缩文件的条件
     */
    private static void init(String[] source, String savePath, String destFileName) {
        if (source == null) {
            throw new IllegalArgumentException("the source file can't be null.");
        }
        if (source.length == 0) {
            throw new IllegalArgumentException("the source file is empty.");
        }

        File sourceFile = null;
        for (int i = 0; i < source.length; i++) {
            sourceFile = new File(source[i]);
            if (!sourceFile.exists()) {
                throw new IllegalArgumentException("the file" + source[i] + "is not exist.");
            }
        }

        File destFile = new File(savePath);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
    }

    public static String[] unZip(String fileName, String savePath) {
        ArrayList<String> fileList = new ArrayList<String>();

        File unzipfile = null;
        InputStream input = null;
        OutputStream output = null;

        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(new File(fileName));
        } catch (Exception e) {
            throw new RuntimeException("Zip File has Exception " + e);
        }
        Enumeration enumeration = zipFile.entries();

        try {
            do {
                ZipEntry entry = (ZipEntry) enumeration.nextElement();
                unzipfile = new File(savePath, entry.getName());

                input = zipFile.getInputStream(entry);
                if (entry.isDirectory()) {
                    unzipfile.mkdirs();

                }

                if (!(entry.isDirectory())) {
                    output = new FileOutputStream(unzipfile);

                    writeFile(input, output);
                    try {
                        if (input != null)
                            input.close();

                        if (output != null)
                            output.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
                fileList.add(entry.getName());
            } while (enumeration.hasMoreElements());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (zipFile != null) {
                    zipFile.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

        String[] str = new String[fileList.size()];
        for (int j = 0; j < fileList.size(); ++j)
            str[j] = ((String) fileList.get(j));

        return str;
    }

}

 

0
0
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    压缩与解压缩文件

    总之,压缩与解压缩文件是信息技术中的基本操作,而霍夫曼编码是其中一种重要的无损压缩方法。理解其原理并能熟练运用,不仅可以优化数据传输,还能在资源有限的情况下提高效率。通过团队项目和个人小结的文档,我们...

    JAVA文件压缩与解压缩实践(源代码+论文)

    在Java编程语言中,文件的压缩与解压缩是常见的数据处理操作,特别是在数据传输、存储优化和备份场景中。本实践项目围绕这个主题展开,包括源代码和相关的论文,为学习者提供了深入理解和应用Java压缩库的机会。以下...

    文件压缩解压缩

    在IT行业中,文件压缩与解压缩是日常工作中常见的操作,特别是在数据传输、存储优化和软件分发等领域。这里我们主要探讨的是一个简单的工具类,它支持zip、rar、tar等多种格式的压缩和解压缩功能,并且经过实际测试...

    压缩与解压缩文件 C++

    在本篇文章中,我们将深入探讨如何使用C++来实现文件的压缩与解压缩功能。 首先,让我们了解一下文件I/O操作。在C++中,我们可以使用`fstream`库来处理文件的读写。例如,要读取一个文件,我们需要创建一个`...

    huffman编码的压缩与解压缩

    在C#环境下实现哈夫曼编码的压缩与解压缩,可以帮助我们理解这一算法的工作原理,同时为文件处理提供了一个实用工具。 哈夫曼编码的核心思想是构建一棵哈夫曼树(Huffman Tree),这是一棵带权路径长度最短的二叉树...

    JAVA文件压缩与解压缩实践(源代码).zip

    JAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩...

    JAVA文件压缩与解压缩实践(源代码+论文).rar

    JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件...

    JAVA文件压缩与解压缩实践(源代码+LW).zip

    JAVA文件压缩与解压缩实践(源代码+LW)JAVA文件压缩与解压缩实践(源代码+LW)JAVA文件压缩与解压缩实践(源代码+LW)JAVA文件压缩与解压缩实践(源代码+LW)JAVA文件压缩与解压缩实践(源代码+LW)JAVA文件压缩与解压缩实践...

    JAVA文件压缩与解压缩实践(源代码+论文).zip

    JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与解压缩实践(源代码+论文)JAVA文件压缩与...

    pb实现压缩与解压缩

    实际的PB压缩解压缩实现可能根据具体需求和设计有所不同,例如可能使用了特定的压缩库(如zlib或libarchive),或者采用了自定义的压缩算法。要深入了解这个话题,最好能够查看和分析提供的源代码。

    JAVA文件压缩与解压缩实践(源代码+lw).rar

    JAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA文件压缩与解压缩实践(源代码+lw).rarJAVA...

    Zip压缩与解压缩

    **Zip压缩与解压缩** Zip是一种广泛使用的文件压缩格式,由菲尔·卡尔森在1989年开发,旨在减少文件占用的磁盘空间,提高数据传输效率。它通过使用不同的压缩算法,如DEFLATE,可以有效地缩小文件大小。在IT领域,...

    (Java毕业设计)JAVA文件压缩与解压缩实践(源代码+).rar

    (Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业...

    java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip

    java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文)....

    C++ 压缩解压缩库

    在给定的标题“C++ 压缩解压缩库”中,我们可以推断这是一个针对C++语言的库,专注于文件或数据的压缩与解压缩功能。描述提到这是为VS2012版本编译的,意味着它可能使用了Visual Studio 2012的编译环境,并且库已经...

    基于哈夫曼编码的文本文件压缩与解压缩

    在"基于哈夫曼编码的文本文件压缩与解压缩"项目中,使用C语言实现这一过程。首先,我们需要分析文本文件中的字符频率,这通常通过遍历整个文本文件计算每个字符的出现次数来完成。接着,根据字符频率构建哈夫曼树。...

Global site tag (gtag.js) - Google Analytics