text文件压缩包解析与下载

//压缩包下载

 private StreamedContent newsTemplate;
//该方法是对压缩包进行下载
    public StreamedContent getNewsTemplate() {
//对多个Text文件进行压缩
        ZipFiles(files,file);
 
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(FaceContextUtil.getCurtExternalContext().getRealPath("") +
                    "/resources/pack/appData.zip");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        newsTemplate = new DefaultStreamedContent(inputStream, "", "appData.zip");
        return newsTemplate;
    }
 
 
 
 
    //压缩多个文件
    public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
        byte[] buf = new byte[1024];
        try {
          //创建zip压缩包输出流
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            // 压缩文件
            for (int i = 0; i < srcfile.length; i++) {
            //读取数组中的文件
                FileInputStream in = new FileInputStream(srcfile[i]);
                // 将该文件名添加到zip输出
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                // Transfer bytes from the file to the ZIP file
                //传送字节到zip包的文件中
                int len;
                while ( (len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                // Complete the entry
                //完成第一个文件的传入
                out.closeEntry();
                in.close();
            }
            // Complete the ZIP file
            //完成压缩,关闭输出流
            out.close();
            System.out.println("压缩完成.");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
 
对zip包进行解压
Enumeration /nju:mê'rei shên/列举 计算 项目
 
 public static void unZipFiles(File zipfile,String descDir){
        try {
            ZipFile zf=new ZipFile(zipfile);
            //Enumeration接口的对象,他生成一系列元素,一次生成一个, 连续调用nextElement方法返回一系列连续元素
            //zf为所有元素,entries为Enumeration的一个元素,ertries.hasMoreElements为是否还存在下一个元素
            for(Enumeration entries=zf.entries();entries.hasMoreElements();){
                ZipEntry entry=(ZipEntry) entries.nextElement();
                String zipEntryName=entry.getName();
                InputStream in=zf.getInputStream(entry);
                OutputStream out=new FileOutputStream(descDir+zipEntryName);
                byte[] buf1=new byte[1024];
                int len;
                while((len=in.read(buf1))>0){
                    out.write(buf1,0,len);
                }
                in.close();
                out.close();
                System.out.println("解压缩完成.");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
 
 
不解压,直接读取压缩包文件 ,根据json转换,加入集合
    public String readZipFile(String file) throws Exception {
        list = new ArrayList<RltVip>();
        list2 = new ArrayList<RltStaR>();
        list3 = new ArrayList<RltAppDev>();
        //创建Zip压缩文件
        ZipFile zipFile = null;
        //创建Zip流
        ZipInputStream zin = null;
        InputStream in = null;
        StringBuffer sbf = null;
        try {
            zipFile = new ZipFile(file);
            in = new BufferedInputStream(new FileInputStream(file));
            zin = new ZipInputStream(in);
            ZipEntry ze;
            sbf = new StringBuffer();
            while ((ze = zin.getNextEntry()) != null) {
                Object lh = (Object) ze;
                String rv = String.valueOf(lh);
                if (ze.isDirectory()) {
                    return null;
                } else {
                    if (rv.equals("ResultVip.txt")) {
                        StringBuffer sBuffer = new StringBuffer();
                        // 这里的判断不能用ze.getSize() > 0, 当文件的大小很小时,会返回-1
                        if (ze.getSize() != 0) {
                            BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                            zipFile.getInputStream(ze), Charset.forName("utf-8")));
                            String line;
                            while ((line = br.readLine()) != null) {
                                sBuffer.append(line + "\r\n");
                                try {
                                    RltVip vip = JsonHelper.CRAZY_GSON.fromJson(line, RltVip.class);
                                    list.add(vip);
                                } catch (Exception e) {
                                    rltVipIndex++;
                                    continue;
                                }
                            }
                            sbf.append(sBuffer);
                            br.close();
                        }
                    } else if (rv.equals("ResultStatistic.txt")) {
                        StringBuffer sBuffer = new StringBuffer();
                        // 这里的判断不能用ze.getSize() > 0, 当文件的大小很小时,会返回-1
                        if (ze.getSize() != 0) {
                            BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                            zipFile.getInputStream(ze)));
                            String line;
                            while ((line = br.readLine()) != null) {
                                sBuffer.append(line + "\r\n");
                                try {
                                    RltStaR rltStaR1 = JsonHelper.CRAZY_GSON.fromJson(line, RltStaR.class);
                                    System.out.printf(rltStaR1.toString());
                                    list2.add(rltStaR1);
                                } catch (Exception e) {
                                    rltStatIndex++;
                                    continue;
                                }
 
                            }
                            sbf.append(sBuffer);
                            br.close();
                        }
                    } else if (rv.equals("ResultDevice.txt")) {
                        StringBuffer sBuffer = new StringBuffer();
                        // 这里的判断不能用ze.getSize() > 0, 当文件的大小很小时,会返回-1
                        if (ze.getSize() != 0) {
                            BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                            zipFile.getInputStream(ze)));
                            String line;
                            while ((line = br.readLine()) != null) {
                                sBuffer.append(line + "\r\n");
                                try {
                                    RltAppDev rlt = JsonHelper.CRAZY_GSON.fromJson(line, RltAppDev.class);
                                    list3.add(rlt);
                                } catch (Exception e) {
                                    rltDevsIndex++;
                                    continue;
                                }
 
                            }
                            sbf.append(sBuffer);
                            br.close();
                        }
                    }
 
                }
            }
 
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (zin != null) {
                    zin.close();
                }
                if (zipFile != null) {
                    zipFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sbf.toString();
    }

zip的打包与解包和包下载的更多相关文章

  1. .zip/.rar打包与解压

    Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于zip linux下提供了zip和unzip程序,zip是 ...

  2. [Java] Java 打包成jar包 和 解压jar包

    解压jar包 jar xf xxx.jar 打包成jar包 方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] ...

  3. Lua学习教程之 可变參数数据打包与解包

    利用table的pack与unpack进行数据打包与解包.測试代码例如以下: print("Test table.pack()----------------"); functio ...

  4. java zip 批量打包(java.util包和apache.tools包)

    /** * 文件批量打包 * @param zipPath 打包路径 * @param files 批量文件 */ public void zipOut(String zipPath,File[] f ...

  5. lambda表达式,filter,map,reduce,curry,打包与解包和

    当然是函数式那一套黑魔法啦,且听我细细道来. lambda表达式 也就是匿名函数. 用法:lambda 参数列表 : 返回值 例: +1函数 f=lambda x:x+1 max函数(条件语句的写法如 ...

  6. MPI 打包与解包函数 MPI_Pack(),MPI_Unpack()

    ▶ MPI 中与数据打包传输有关的几个函数 ● 函数 MPI_Pack() 与 MPI_Unpack() 的原型 MPI_METHOD MPI_Pack( _In_opt_ const void* i ...

  7. CentOS7 tar打包工具 打包,解包,打包压缩,打包解压缩

    tar命令 選項與參數: -c :建立打包檔案,可搭配 -v 來察看過程中被打包的檔名(filename) -t :察看打包檔案的內容含有哪些檔名,重點在察看『檔名』就是了: -x :解打包或解壓縮的 ...

  8. struct:二进制数据结构的打包与解包

    介绍 struct模块包括一些函数,这些函数可以完成字节串与原生Python数据类型(如数字和字符串)之间的转换 函数与Struct类 struct提供了一组处理结构值的模块级函数,另外还有一个Str ...

  9. Unix系统解压tar包时出现@LongLink错误

    Unix系统上使用tar命令解压tar包后,多了一个@LongLink的文件,并且原来的tar包解压后不完整.网上查了下,原因是AIX系统上tar命令自身的一个缺陷.解决办法:把该tar包上传到lin ...

随机推荐

  1. linux shell 中的位置变量

    对于linux shell 中的位置变量,我一直以来都是吐不出来又咽不下去,每次看到都不懂,不懂了就去百度google,看完了又忘,真是慢性咽炎啊.现在认真想想也是,其实自己一直以来都没有好好学习过, ...

  2. LPC1768之时钟

    一锁相环和CPU时钟. CPU时钟=锁相环0输出/CPU时钟配置寄存器的预分频值即:Fcpu=Fcco/CCLKCFG+1.锁相环可以把外部时钟倍频到较高频率,PLL0输出频率是: Fcco = (2 ...

  3. ANT教程经典

    Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作. 一.            ...

  4. Python进阶08 异常处理

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 本文特别感谢kylinfish的纠正,相关讨论见留言区. 异常处理 在项目开发中, ...

  5. 离线使用echarts及一些细节

    最近要做图表,用js起来太麻烦,所以就找些开源的库来用,发现echarts挺不错, echarts的文档把所有东西都说的很明白了,直接下载zip包,要是想离线使用的话只需要引用下载包里面的dist文件 ...

  6. 采访ServiceStack的项目领导Demis Bellot——第2部分(转)

    ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第2部分中,我 ...

  7. windows平台快速安装 matplotlib

    版本:Python2.7.9 Pip 1.5.6 如果没有安装VC2008以上版本,就需要工具Microsoft Visual C++ Compiler for Python 2.7 http://w ...

  8. 国内Lua先驱的Lua源码总结

    http://www.codingnow.com/temp/readinglua.pdf <Lua源码欣赏> By 云风

  9. OpenCV数据结构:CvMat,IplImage,CvArr的应用

    最近看HS算法时,发现在新的OpenCV3.0已经移除了该算法,于是不得不看老版的.这其中涉及到了CvMat,CvArr,IplImage的使用.关于这三个结构与Mat的转换关系,总结如下: (1)派 ...

  10. Nginx作为静态内容服务器(Windows环境)

    1.简单安装 1)下载 http://nginx.org/en/download.html 2)解压后的路径 E:\Study\nginx\nginx-1.7.6 3)执行nginx.exe,访问ht ...