Java implementation of the server file package zip and download the example (side packaging side download) _java

Source: Internet
Author: User
Tags cos crc32 flush stringbuffer

Using this method, you can package files on the fly, while packaging and transmitting, without using any caching, let the user 0 wait!

Copy Code code as follows:

/**
*
* Mysocket Client Socket
* @param file to be packaged folder or file
* @param filename packaged and downloaded
* @throws IOException
*/

private void down (file file, String fileName) throws IOException {
OutputStream outputstream = Mysocket.getoutputstream ();
StringBuffer sb = new StringBuffer ("http/1.1 ok\r\n");
Sb.append ("server:java/1.1\r\n");
Sb.append ("content-type:application/octet-stream;charset=utf-8\r\n");
Sb.append ("user-agent:mozilla/4.0" (compatible; MSIE6.0; Windows NT 5.0) \ r \ n ");
Sb.append ("content-disposition:attachment; Filename= "+ fileName
+ "\ r \ n");
Sb.append ("transfer-encoding:chunked\r\n");
Sb.append ("connection:keep-alive\r\n\r\n");
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 ("0\r\n\r\n". GetBytes ());//transfer-encoding:chunked Transfer end tag
Outputstream.flush ();
Outputstream.close ();
System.out.println ("Download Stop");
try {
Mysocket.close ();
catch (Throwable t) {
}
}

Copy Code code as follows:

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;
&NBSP}
 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); The
   out.close ();//must be closed so that the zip end information is written, or the zip file is incomplete. To continue writing, you can override the Outputstream.close () method
  &NBSP} catch (Exception e) {
   throw new runtimeexception (e);
 &NBSP}
 }

private void compress (file file, zipoutputstream out, String basedir) {
Judge 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++) {
/** recursion *
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);
}
}
}

Copy Code code as follows:

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 = "1\r\n". GetBytes ();
Final byte[] Rnbytes = "\ r \ n". GetBytes ();

public void Write (int b) throws IOException {
Out.write (onebytes);//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
}

/**
* Rewrite the method, otherwise the outputstream will be closed, other data <br/>
* (such as transfer-encoding:chunked transmission end tag) can not continue to write
*/
public void Close () throws IOException {
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.