I/O流、ZIP文档

1) ZIP文档通常以压缩格式存储一个或多个文档。在Java中可以用ZipInputStream读入ZIP文档(即解压文件流),用ZipOutputStream写入ZIP文档(即压缩文件流),无论解压或者压缩都需要创建ZipEntry对象(因为ZipEntry对象是流中必须要用的文件项)。压缩时创建ZipEntry对象,需要传入文件名;解压时创建ZipEntry对象可以调用该对象的getName()获取文件名称,每次ZipEntry项使用完之后需要调用closeEntry(),以便调用getNextEntry()或者putNextEntry()获得下个ZipEntry对象。最后无论解压亦或者是压缩整个过程操作完之后记得调用close关闭流释放资源。ZipInputStream和ZipOutputStream属于java.util.zip包中。

如果压缩的文件是有多级目录的,且解压时需要按照多级目录的要求解压,那么需要先手动创建多级目录,然后在解压文件,否则会报异常(找不到指定的系统路径)

下面是一个压缩和解压的例子

    /**
     * 同级文件压缩
     */
    public static void zipCompression() {

        try {
            // 文件的根路径
            String fileRootPath = "G:\\";

            // 已有的两个文件
            String[] files = { "1.txt", "2.txt" };

            // 创建一个zip文件
            File file = Paths.get(fileRootPath, "test.zip").toFile();

            // 创建ZipOutputStream 对象
            ZipOutputStream zipOutputStream = new ZipOutputStream(
                    new FileOutputStream(file));

            // 多个文件, 同级文件压缩
            for (String fileName : files) {

                ZipEntry zipEntry = new ZipEntry(fileName);
                zipOutputStream.putNextEntry(zipEntry);

                // 读文件
                String filePath = fileRootPath + fileName;
                FileInputStream fileInputStream = new FileInputStream(filePath);
                byte[] b = new byte[1024];
                int size = 0;
                // 写入压缩文件
                while ((size = fileInputStream.read(b)) != -1) {
                    zipOutputStream.write(b, 0, size);
                }
                // 关闭 ZipEntry对象
                zipOutputStream.closeEntry();
                // 关闭文件流
                fileInputStream.close();
            }
            // 关闭ZIP 流
            zipOutputStream.close();

        } catch (Exception exception) {

        }
    }
    /**
     * 同级文件解压
     */
    public static void zipDecompression() {
        try {
            String fileZipName = "G:\\test.zip";

            // 创建ZipInputStream 对象
            FileInputStream fileInputStream = new FileInputStream(fileZipName);
            ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
            ZipEntry zipEntry = null;

            // 得到ZipEntry项,创建一个文件,将内容写入文件
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                String newFileName = "G:\\" + zipEntry.getName();
                FileOutputStream fileOutputStream = new FileOutputStream(
                        newFileName);
                byte[] b = new byte[1024];
                int size = 0;
                while ((size = zipInputStream.read(b)) != -1) {
                    fileOutputStream.write(b, 0, size);
                }

                // 关闭文件流
                fileOutputStream.close();

                // 关闭当前ZipEntry项 调用getNextEntry() 获取下一个ZipEntry项
                zipInputStream.closeEntry();
            }
            zipInputStream.close();

        } catch (Exception exception) {

        }

    }

 

posted @ 2017-05-05 15:26  YSP  阅读(377)  评论(0编辑  收藏  举报