Java implementation of the server file package zip and download the sample of of of package while downloading

  • 2020-04-01 03:14:27
  • OfStack

Using this method, the file can be packaged in an instant, while the packaging transfer, do not use any cache, let the user zero wait!



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 '0\r\n\r\n'");
 outputStream.write("0rnrn".getBytes());//Transfer-encoding: chunked Transfer end tag
 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() + " Does not exist! ");
  try {
   CheckedOutputStream cos = new CheckedOutputStream(outputStream,
     new CRC32());
   ZipOutputStream out = new ZipOutputStream(cos);
   String basedir = "";
   compress(file, out, basedir);
   out.close();//Must be closed to write the end of the zip, or the zip file is incomplete. To continue writing, override the outputstream.close () method
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
 private void compress(File file, ZipOutputStream out, String basedir) {
  //Determine whether it is a directory or a file
  if (file.isDirectory()) {
   System.out.println(" Compression: " + basedir + file.getName());
   this.compressDirectory(file, out, basedir);
  } else {
   System.out.println(" Compression: " + basedir + file.getName());
   this.compressFile(file, out, basedir);
  }
 }
 //Compress a directory
 private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
  if (!dir.exists())
   return;
  File[] files = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   
   compress(files[i], out, basedir + dir.getName() + "/");
  }
 }
 //Compress a file
 private void compressFile(File file, ZipOutputStream out, 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, 0, BUFFER)) != -1) {
    out.write(data, 0, 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);//The number of bytes 1 + CRLF
  out.write(b);//Data entity
  out.write(rnBytes);//CRLF
 }
 public void write(byte[] b) throws IOException {
  out.write(Integer.toHexString(b.length).getBytes());//Number of bytes, hexadecimal
  out.write(rnBytes);//CRLF
  out.write(b);//Data entity
  out.write(rnBytes);//CRLF
 }
 public void write(byte[] b, int off, int len) throws IOException {
  out.write(Integer.toHexString(len - off).getBytes());//Number of bytes, hexadecimal
  out.write(rnBytes);//CRLF
  out.write(b, off, len);//Data entity
  out.write(rnBytes);//CRLF
 }
 /**
  *  Override the method , Otherwise, OutputStream Will be closed , Other data <br/>
  * ( Such as Transfer-Encoding: chunked End of transmission flag ) I can't write any more 
  */
 public void close() throws IOException {
 }
}


Related articles: