国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > java基础 > java压缩流的压缩与解压

java压缩流的压缩与解压

头像

小妮浅浅

2021-02-20 09:43:282865浏览 · 0收藏 · 0评论

本教程操作环境:windows7系统、java10版,DELL G3电脑。

1.概念

压缩流可以将输入的数据变为压缩格式后进行输出,或者读取压缩格式的数据后,解压为正常数据。

2.压缩步骤

(1)生成一个压缩类对象,这个对象来自于一个".zip"的文件,通过它产生一ZipOutputStream对象;

(2)生成压缩对象入口,因为需要被压缩的文件不止一个。需要用ZipEntry方法生成压缩入口文件后才能放进压缩文件;

(3)用putNextEntry将压缩入口放入压缩文件;

(4)将文件内容写入了out.write(),将压缩入口和文件流关闭。

3.目录压缩

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
public class ZipStreamExam2 {
    public static void main(String[] args) {
        try {
            File file = new File("d:\\zipmultidir");
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("d:\\zipmultidir.zip")));
            zipDir(file, zos, file);
            zos.flush();
            zos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //压缩一个目录至zip文件
    private static void zipDir(File dir, ZipOutputStream zos, File rootDir) throws IOException {
        if (!dir.isDirectory())
            return;
 
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                System.out.println(files[i].getAbsolutePath());
                String now = files[i].getAbsolutePath();
                String root = rootDir.getAbsolutePath();
                String name = now.substring(root.length() + 1);
                System.out.println(name);
 
                FileInputStream fis = new FileInputStream(files[i]);
 
                byte buf[] = new byte[1024];
                int len = 0;
                ZipEntry ze = new ZipEntry(name);
                zos.putNextEntry(ze);
                while ((len = fis.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                fis.close();
            } else if (files[i].isDirectory()) {
                zipDir(files[i], zos, rootDir);
            }
        }
    }
}

4.解压到目录

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
/**
 * Created by test2 on 2016/8/19.
 */
public class ZipStreamExam3 {
    public static void main(String[] args) {
        try {
            File srcFile = new File("d:\\zipmultidir.zip");
            System.out.println(srcFile.getCanonicalPath());
            String curDir = srcFile.getParent()+File.separator+"destDir"+File.separator;
 
            ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile)));
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            int len = 0;
            while ((ze = zipInputStream.getNextEntry()) != null) {
                String filePath = curDir + ze.getName();
                File destFile = new File(filePath);
                File destDir = new File(destFile.getParent());
                if(!destDir.exists()){
                    destDir.mkdirs();
                }
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));
                while ((len = zipInputStream.read(buf)) != -1) {
                    bufferedOutputStream.write(buf, 0, len);
                }
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            }
            zipInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上就是java压缩流的压缩与解压方法,在学习了压缩流的基础知识后,就压缩、解压的方法分别带来代码展示,学会后就可以运行体验压缩流的用法了。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

内地禁止上映的6部大电影| 人野兽马狗猪大全| 女子排尿视频大全| 丁字裤阴唇肥大的地方叫什么| 暗黑吃瓜网官网CGW.73| 《不戴套瑜伽教练3》| 女人被灌满精子后多久排卵期| 他用舌头给我6次高潮| 偏偏宠爰| 苏玥厨房张开腿疯狂迎合视频| 美国伦理《轻佻寡妇》| 校花被群JAN又粗又大H漫画| 《艳妻互换》完整版| 小妈爱上继子免费观看电视剧| 无码人妻久久一区二区| 日本大片又大又好看的PPT| 你们日的时候都说什么| 欧美叔叔别撩我免费观看电视剧| 绝色儿媳在线观看免费版电视剧 | 国产一区| 女人被男人进入后的心理变化| 公与媳中文字幕乱码的预防方法| 吃春晚药后妈妈疯了怎么对付| 1.120分钟免费观看电视剧大全| 女生的全身照曝光| 在线观看免费高清视频大全追剧| 中字《喝醉的丈夫被结城送回家》 | 《好好疼爱里面》日剧动漫| 雨后小故事贴吧| 门卫老头吮她的花蒂的主演是谁| 高压监狱在线观看免费高清完整| 阴部按摩出水太尴尬怎么办| 男生女生拆拆很痛的轮滑鞋免费下. | 黑料吃瓜| 一线产区和二线产区的标准图| 《人奶魔劫》完整版| 老和尚猛挺进她莹莹的体导演是谁| 你丈夫比我老公更厉害| 光溜溜瑜伽全套视频大全| 《如狼似虎的女人》大结局| 苍老师《荷尔蒙》