通过输出流在前台显示zip包中的文件信息

前台打印
       String previewXML = showZipFile(filePath); ServletOutputStream outputStream = response.getOutputStream(); response.reset();// 清空输出流 response.setHeader("Content-disposition", "attachment; filename=" + filePath.substring(filePath.lastIndexOf("\")+1));// 设定输出文件头 response.setContentType("application/xml");// 定义输出类型 outputStream.write(previewXML.getBytes()); outputStream.flush(); outputStream.close();

拿到文件数据

public  String showZipFile(String filePath)  {  
           ZipFile zf;
           StringBuffer sb = new StringBuffer();
           String substring = filePath.substring(filePath.lastIndexOf("\")+1);
           filePath = filePath.substring(0,filePath.lastIndexOf("\")+1);
            try {
            zf = new ZipFile(filePath);
            FileInputStream in = new FileInputStream(filePath);
            ZipInputStream zin = new ZipInputStream(in);  
            ZipEntry ze;
             while ((ze = zin.getNextEntry()) != null) {  
                 if (ze.isDirectory()) {
                 } else {  
                    String name = ze.getName();
                    
                    if(name.equals(substring)) {
                     long size = ze.getSize();  
                     if (size > 0) {  
                         BufferedReader br = new BufferedReader(  
                                 new InputStreamReader(zf.getInputStream(ze)));  
                         String line;  
                         while ((line = br.readLine()) != null) {  
                             sb.append(line);
                         }  
                         br.close();  
                     }  
                    }
                     continue;
                 }  
             }  
             zin.closeEntry();  
             in.close();
             zin.close();
             return sb.toString();
            } catch (IOException e) {
            } 
            return null;
         }  
原文地址:https://www.cnblogs.com/ShaoXin/p/7771911.html