加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java实现服务器文件打包zip并下载的示例(边打包边下载)

发布时间:2020-12-14 05:13:49 所属栏目:Java 来源:网络整理
导读:使用该方法,可以即时打包文件,一边打包一边传输,不使用任何的缓存,让用户零等待! 复制代码 代码如下: /** * * mySocket 客户端 Socket * @param file 待打包的文件夹或文件 * @param fileName 打包下载的文件名 * @throws IOException */ private void down(

使用该方法,可以即时打包文件,一边打包一边传输,不使用任何的缓存,让用户零等待!

复制代码 代码如下:

/**
 *
 * mySocket 客户端 Socket
 * @param file 待打包的文件夹或文件
 * @param fileName 打包下载的文件名
 * @throws IOException
 */

private void down(File file,String fileName) throws IOException {
 OutputStream outputStream = mySocket.getOutputStream();
 StringBuffer sb = new StringBuffer("HTTP/1.1 200 OKrn");
 sb.append("Server: java/1.1rn");
 sb.append("Content-Type:application/octet-stream;charset=UTF-8rn");
 //sb.append("User-Agent: Mozilla/4.0 (compatible;MSIE6.0;Windows NT 5.0)rn");
 sb.append("Content-Disposition: attachment; filename=" + fileName
   + "rn");
 sb.append("Transfer-Encoding: chunkedrn");
 sb.append("Connection: Keep-Alivernrn");
 outputStream.write(sb.toString().getBytes());
 outputStream.flush();
 ZipCompressor zipCompressor = new ZipCompressor(new MyOutputStream(
   outputStream));
 zipCompressor.compress(file);
 System.out.println("zip end");  
 System.out.println("write '0rnrn'");
 outputStream.write("0rnrn".getBytes());//Transfer-Encoding: chunked传输结束标记
 outputStream.flush();
 outputStream.close();
 System.out.println("download stop");
 try {
  mySocket.close();
 } catch (Throwable t) {
 }
}

复制代码 代码如下:

package cn.liangjintang.webserver.zipFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompressor {
 static final int BUFFER = 8192;
 private OutputStream outputStream;
 public ZipCompressor(MyOutputStream outputStream) {
  this.outputStream=outputStream;
 }
 public void compress(File file) {
  if (!file.exists())
   throw new RuntimeException(file.getAbsolutePath() + "不存在!");
  try {
   CheckedOutputStream cos = new CheckedOutputStream(outputStream,
     new CRC32());
   ZipOutputStream out = new ZipOutputStream(cos);
   String basedir = "";
   compress(file,out,basedir);
   out.close();//必须关闭,这样才会写入zip的结束信息,否则zip文件不完整.若想继续写入,可重写outputStream.close()方法
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

 private void compress(File file,ZipOutputStream out,String basedir) {
  //判断是目录还是文件
  if (file.isDirectory()) {
   System.out.println("压缩:" + basedir + file.getName());
   this.compressDirectory(file,basedir);
  } else {
   System.out.println("压缩:" + basedir + file.getName());
   this.compressFile(file,basedir);
  }
 }

 // 压缩一个目录
 private void compressDirectory(File dir,String basedir) {
  if (!dir.exists())
   return;

  File[] files = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   /** 递归 */
   compress(files[i],basedir + dir.getName() + "/");
  }
 }

 //压缩一个文件
 private void compressFile(File file,String basedir) {
  if (!file.exists()) {
   return;
  }
  try {
   BufferedInputStream bis = new BufferedInputStream(
     new FileInputStream(file));
   ZipEntry entry = new ZipEntry(basedir + file.getName());
   out.putNextEntry(entry);
   int count;
   byte data[] = new byte[BUFFER];
   while ((count = bis.read(data,BUFFER)) != -1) {
    out.write(data,count);
   }
   bis.close();
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
}

复制代码 代码如下:

package cn.liangjintang.webserver.zipFile;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MyOutputStream extends FilterOutputStream {
 public MyOutputStream(OutputStream out) {
  super(out);
 }

 final byte[] oneBytes = "1rn".getBytes();
 final byte[] rnBytes = "rn".getBytes();

 public void write(int b) throws IOException {
  out.write(oneBytes);//字节数1+CRLF
  out.write(b);//数据实体
  out.write(rnBytes);//CRLF
 }

 public void write(byte[] b) throws IOException {
  out.write(Integer.toHexString(b.length).getBytes());//字节数,十六进制
  out.write(rnBytes);//CRLF
  out.write(b);//数据实体
  out.write(rnBytes);//CRLF
 }

 public void write(byte[] b,int off,int len) throws IOException {
  out.write(Integer.toHexString(len - off).getBytes());//字节数,十六进制
  out.write(rnBytes);//CRLF
  out.write(b,off,len);//数据实体
  out.write(rnBytes);//CRLF
 }

 /**
  * 重写该方法,否则OutputStream会被关闭,其他的数据<br/>
  * (如Transfer-Encoding: chunked传输结束标记)就不能再继续写入了
  */
 public void close() throws IOException {
 }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读