解压缩文件数据流直接生成文件--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数据流。

你可能感兴趣的:(解压缩文件数据流直接生成文件--JAVA)