<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app"%>
<%@   page   import= "java.io.* "   %> 
<%@   page   import= "java.util.zip.* "   %> 
<HTML> 
<BODY> 
<%  
response.setHeader( "Content-disposition", "attachment;filename="+ "book.zip ");   
response.setContentType("application/x-gzip;charset=UTF-8");


byte[] buf = new byte[1024];
ByteArrayInputStream bin = new ByteArrayInputStream("aasssssssssssssssssa".getBytes()); 
ByteArrayOutputStream baos = new ByteArrayOutputStream();//字符串输出流
int len = 0;
while ((len = bin.read(buf)) != -1) {
    baos.write(buf, 0, len); 
}

ZipOutputStream zipOut = new ZipOutputStream(baos); //zip流包装
zipOut.putNextEntry(new ZipEntry("b.txt"));

OutputStream outa = response.getOutputStream();
outa.write(baos.toByteArray());//向客户端输出被包装成zip的流
response.flushBuffer(); 

zipOut.close();
baos.close(); 
bin.close();
outa.close();
%> 
</BODY> 
</HTML>

解决方案 »

  1.   

    如题,可是下载完,总报出这个错误“不可预料的压缩文件末端”,无法打开zip文件。。求解
      

  2.   


    ByteArrayOutputStream baos = new ByteArrayOutputStream();//字符串输出流
    int len = 0;
    while ((len = bin.read(buf)) != -1) {
    baos.write(buf, 0, len); 
    }ZipOutputStream zipOut = new ZipOutputStream(baos); //你这里的baos对象已经write完了,这里没了。
    zipOut.putNextEntry(new ZipEntry("b.txt"));
      

  3.   

    IO流用的不熟悉、、那还请教怎么把字符串直接压缩成zip输出到客户端。
      

  4.   

        try {       // Create the ZIP file
          ZipOutputStream out = new ZipOutputStream(new ByteArrayOutputStream("aa").getBytes());
          
                 // Compress the files
            FileInputStream in = new FileInputStream("aaaaaaaaaaa");         // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry("b.txt"));         // Transfer bytes from the file to the ZIP file
            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();     
        }
        catch (IOException e) {
          e.printStackTrace();
        }这样我没有测试。。你可以试试
      

  5.   


    response.setHeader( "Content-disposition", "attachment;filename="+ "book.zip ");   
    response.setContentType("application/x-gzip;charset=UTF-8");

    OutputStream outStream = response.getOutputStream();
    ByteArrayInputStream bin = new ByteArrayInputStream("aassssssssssssss中文文sssa".getBytes()); 
    ZipOutputStream zipOut = new ZipOutputStream(outStream); //zip流包装
    zipOut.putNextEntry(new ZipEntry("b.txt"));

    byte[] buf = new byte[1024];
    int len;
        while ( (len = bin.read(buf)) > 0) {
            zipOut.write(buf, 0, len);
        }
    response.flushBuffer(); 
    zipOut.close();
    bin.close();
    outStream.close();
    找到方法了。很简单的api、、
      

  6.   


    上面写错了
    我写了一个 测试成功 public static void ZipFiles(String zipfile) {
        byte[] buf = new byte[1024];
        try {
          // Create the ZIP file
          ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
          // Compress the files
          //for (int i = 0; i < srcfile.length; i++) {
          ByteArrayInputStream in = new ByteArrayInputStream("aaaa".getBytes());
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry("test.txt"));
            // Transfer bytes from the file to the ZIP file
            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();
         
        }
        catch (IOException e) {
          e.printStackTrace();
        }
      } 
      

  7.   


    要知道你第一次失败的原因:
    你应该先包装好流再读输出,你最开始的代码是字符串输出流写出去了,才用zip流包装,所以失败。