栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

文件解压和压缩io流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文件解压和压缩io流

  • 参考
  • https://www.cnblogs.com/lrh-xl/p/5509005.html
  • https://www.wenjiangs.com/search?word=ZipEntry
  • java.util.zip
    文件的压缩和解压,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还可以对文件夹进行压缩和解压。
    ZipInputStream位于java.util.zip包下。
    -先上示例在详解
// 将文件压缩
File file = new File("D:\io\zip\hah.doc");
        File file1 = new File("D:\io\zip\bbb.docx");
        File file2 = new File("D:\io\zip\test.zip");

        //创建文件输入流
        FileInputStream fileinput = new FileInputStream(file1);
        //开启缓存流
        BufferedInputStream bufferedinput = new BufferedInputStream(fileinput);
        //创建一个文件输出流
        FileOutputStream fileout = new FileOutputStream(file2);
        //创建一个压缩输出流
        ZipOutputStream zipout = new ZipOutputStream(fileout);
        //开启输出缓存
        BufferedOutputStream bufferedout = new BufferedOutputStream(zipout);
        
        zipout.putNextEntry(new ZipEntry("bbb.docx"));
        byte[] bytes = new byte[1024];
        while (true){
            int read = bufferedinput.read(bytes);
            if(read == -1){
                break;
            }
            bufferedout.write(bytes,0,read);
        }
        //关闭缓存流
        bufferedinput.close();
        bufferedout.close();
  • 目录压缩
package com.kuangsheng.mybatis_plus.test;

import java.io.*;
import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Test  {
    public static void main(String[] args) throws IOException {
        //这个示例是将目录压缩
        File file = new File("D:\io\demo");

        File file1 = new File("D:\io\zip\bbb.docx");
        File file2 = new File("D:\io\demo.zip");

        //创建压缩输出流 意思就是设置终点
        ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(file2));

        //判断数据源是不是一个目录
        if(file.isDirectory()){
            //获取目录下中所有的文件
            File[] files = file.listFiles();
            //遍历目录下所有文件变成流哈
            for(int a=0;a 
  • 压缩包解压
package com.kuangsheng.mybatis_plus.test;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ZipDemo {
    public static void main(String[] args) throws IOException {
        
        //数据源就是压缩包
        File file = new File("D:\io\demo.zip");

        //压缩包类
        ZipFile zipFile = new ZipFile(file);
        //将压缩包创建成压缩包流
        ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
        //创建存放数据的对象,
        ZipEntry entry = null;
        while((entry=zipInput.getNextEntry())!= null){
            System.out.println("源文件"+entry.getName());
            //创建目的地
            File outputfile = new File("D:\io\good\"+entry.getName());
            System.out.println(outputfile.getParentFile().exists());
            if(!outputfile.getParentFile().exists()){
                outputfile.getParentFile().mkdirs();
            }
            System.out.println(outputfile.getParentFile().exists());
            if(!outputfile.exists()){
                outputfile.createNewFile();
            }
            //用缓存流来传输数据
            BufferedInputStream bufferedinput = new BufferedInputStream(zipFile.getInputStream(entry));
            BufferedOutputStream bufferedoutput = new BufferedOutputStream(new FileOutputStream(outputfile));
            byte[] bytes = new byte[1024];
            while(true){
                int len = bufferedinput.read(bytes);
                if(len ==-1)
                    break;
                bufferedoutput.write(bytes,0,len);
            }
            //关闭流
            bufferedinput.close();
            bufferedoutput.close();
        }
        zipInput.close();

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678139.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号