韩漫免费漫画在线观看方法,《好好疼爱里面》免费看,年轻漂亮的女士护士内衣,妈妈醉酒后把我当爸爸电视剧

文章 > 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| 水蜜桃免费高清电视剧大全 | 引诱沉沦BY肉桂笔趣阁小说 | 男性23厘米尺寸图片| 乱亲H女乱秽XXXX| 恋爱之瘾台湾剧| 成全在线观看免费高清电视剧| 亲亲抱抱拔萝 卜| 快拨出天我是你母亲歌词监控| 老公不在我和公发| 千王之王电影| 干别人老婆还当面打电话| 美国禁忌6 结局| 陌陌影视在线观看高清完整版| 《急救护士1982满天星》| 台球室的特殊待遇2HD| 十九岁暴躁少女CSGO免费观看| 麻豆成人啪啪色婷婷久久| 120分钟免费观看电视剧大全| 苹果13参数| 欧美风高级情侣ID| 恋爱之瘾电影完整版的免费| 《蜜汁樱桃》笔趣阁全文免费阅读 | 45岁老阿姨喷了三次尿素乳育| 男女主亲密缠绕的画面真人版| 主人~我错了请把它关掉歌词 | 久久久无码AV精品夜夜挺价格| 腿张开我要添到你高潮H漫画软件| 罗曼史1999| 啵啵浏览器| 成品免费PPT网站入口| 老公拿狗给我配| 妈妈的朋友7| 双乳压在落地窗前做H| 巨乳童颜| 同学的母亲中字ID| 少女配狗视频大全播放| 《黏糊糊的你》免费观看| 杨贵妃(台湾绝版)