解压缩文件数据流直接生成文件--JAVA

1.搬运这个代码的目的:不想看见网上的各种曲线救国,不存在什么获取文件数据流先写成文件再解压缩。

2.This is A DEMO :


/**

* 根据客户端传过来的数据流直接生成文件,不需要写成文件再解压缩

*@paramdatas

*@paramtargetFileDir

*@throwsException

*/

public voidunZipByte(byte[] datas,String targetFileDir)throwsException {

        // System.setProperty("sun.zip.encoding",

        // System.getProperty("sun.jnu.encoding"));

        ByteArrayInputStream bis =newByteArrayInputStream(datas);

        ZipInputStream zis =newZipInputStream(bis);

        ZipEntry entry =null;

        while((entry = zis.getNextEntry()) !=null) {

                // System.out.printf("条目信息: 名称%1$b, 大小%2$d, 压缩时间%3$d \n",

                // entry.getName(), entry.getSize(), entry.getTime());

                        if(entry.isDirectory()) {// is dir

                            // System.out.println(entry.getName() + "是一个目录");

                                File f =newFile(targetFileDir + File.separator+ entry.getName());

                                if(!f.exists())

                                            f.mkdirs();

                                    }else{//

                                    byte[] data = getByte(zis);// 获取当前条目的字节数组

                                    InputStream is =newByteArrayInputStream(data);// 把当前条目的字节数据转换       成Inputstream流

                                    String[] names = entry.getName().split("/");

                                String path = targetFileDir + File.separator;

                                    path +=join(names, File.separator);

                                            //System.out.println(path);

                                            File file =newFile(path);

                                            if(!file.exists()) {

                                            file.createNewFile();

                                            toWrite(is, file);

                                            }

                                }

                        }

            }

/**

* 向file文件写入字节

*@paramins

*@paramfile

*/

public static voidtoWrite(InputStream ins, File file) {

try{

        OutputStream os =newFileOutputStream(file);

        intbytesRead =0;

        byte[] buffer =new byte[8192];

        while((bytesRead = ins.read(buffer,0,8192)) != -1) {

                os.write(buffer,0, bytesRead);

            }

            os.close();

            ins.close();

            }catch(Exception e) {

                e.printStackTrace();

                }

}

/**

* 获取条目byte[]字节

*@paramzis

*@return

*/

public byte[] getByte(InflaterInputStream zis) {

try{

            ByteArrayOutputStream bout =newByteArrayOutputStream();

            byte[] temp =new byte[1024];

                byte[] buf =null;

                intlength =0;

                while((length = zis.read(temp,0,1024)) != -1) {

                bout.write(temp,0, length);

        }

                buf = bout.toByteArray();

                bout.close();

                returnbuf;

                }catch(IOException e) {

                e.printStackTrace();

                return null;

        }

}


public staticString join(Object[] o, String flag) {

        StringBuffer str_buff =newStringBuffer();

        for(inti =0, len = o.length; i < len; i++) {

                str_buff.append(String.valueOf(o[i]));

                if(i < len -1)

                str_buff.append(flag);

            }

        returnstr_buff.toString();

}


调用方法:

      1.直接获取一个文件的输出流BYTE【】,加上文件的输出目录

      2.或者直接获取一个文件的数据流,在unZipByte方法里直接通过ZIp转化成Zip数据流。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 10,106评论 2 95
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,779评论 18 399
  • "江歌被害案"再次让人们质疑善良的意义,有人觉得"人善被人欺"是绝对真理。 很多父母开始担忧:一直在教孩子追求真善...
    糖果妈妈爱孩子阅读 332评论 0 0
  • 原以为今年的冬天会来得很早,可直到如今还让人察觉不到一丝的气息。让人不免的有些失望。可一想,如果果真冬天如期而至,...
    海的王子阅读 210评论 0 0