Implementation code:
Definition file root path: Tomcat temp path + timestamp
String dirName = System.getproperty ("Java.io.tmpdir") + System.currenttimemillis ();
String fileName = "Xxx.zip";
Response.setcontenttype ("Application/zip");
Response.setcharacterencoding ("UTF-8");
String dfilename = null;
Dfilename = new String (filename.getbytes ("GB2312"), "iso_8859_1");
Response.setheader ("Content-disposition", "attachment;filename=" + dfilename);
OutputStream Ouputstream = Response.getoutputstream ();
Zipoutputstream Zos = new Zipoutputstream (ouputstream);
Zos.setencoding ("GBK");//Solve the Chinese garbled problem, do not add this sentence will be inexplicably more a garbled empty folder
Cziputil.zip (Zos, "", dirName);
Zos.flush ();
Zos.close ();
Ouputstream.flush ();
Ouputstream.close ();
Compression Tool Class:
Package huihai.sims.common.util;
Import Huihai.sims.common.util.CCommon;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipFile;
Import Org.apache.tools.zip.ZipOutputStream;
/**
* Compression/Decompression ZIP packet processing class, the decompression process has Chinese garbled problem
*/
public class Cziputil {
/**
* Compression
* @param zipfilename Compression generated zip package file name-with path, if null or empty by default production of compressed file name by file name
* @param relativepath relative path, default is empty
* Absolute path to @param directory file or directory
* @throws FileNotFoundException
* @throws IOException
*/
public static void Zip (String zipfilename, String relativepath, String directory) throws FileNotFoundException, Ioexcepti on {
String fileName = zipfilename;
if (FileName = = NULL | | Filename.trim (). Equals ("")) {
File temp = new file (directory);
if (Temp.isdirectory ()) {
FileName = Directory + ". zip";
} else {
if (Directory.indexof (".") > 0) {
FileName = directory.substring (0, directory
. LastIndexOf ("."))
+ "zip";
} else {
FileName = Directory + ". zip";
}
}
}
Zipoutputstream Zos = new Zipoutputstream (
New FileOutputStream (FileName));
try {
Zip (Zos, relativepath, directory);
} catch (IOException ex) {
Throw ex;
} finally {
if (null! = Zos) {
Zos.close ();
}
}
}
/**
* Compression
* @param zos Compressed output stream
* @param relativepath relative path
* @param absolutpath file or folder absolute path
* @throws IOException
*/
public static void Zip (Zipoutputstream zos, String relativepath, String absolutpath) throws IOException {
File File = new file (Absolutpath);
if (File.isdirectory ()) {
file[] files = file.listfiles ();
for (int i = 0; i < files.length; i++) {
File tempfile = files[i];
if (Tempfile.isdirectory ()) {
String Newrelativepath = RelativePath + tempfile.getname ()
+ File.separator;
Createzipnode (Zos, Newrelativepath);
Zip (Zos, Newrelativepath, Tempfile.getpath ());
} else {
ZipFile (Zos, Tempfile, RelativePath);
}
}
} else {
ZipFile (Zos, file, RelativePath);
}
}
/**
* Compressed Files
* @param zos Compressed output stream
* @param file Object
* @param relativepath relative path
* @throws IOException
*/
private static void ZipFile (Zipoutputstream zos, file file, String RelativePath) throws IOException {
ZipEntry entry = new ZipEntry (RelativePath + file.getname ());
Zos.putnextentry (entry);
InputStream is = null;
try {
is = new FileInputStream (file);
int buffersize = 2 << 10;
int length = 0;
byte[] buffer = new Byte[buffersize];
while (length = is.read (buffer, 0, buffersize)) >= 0) {
Zos.write (buffer, 0, length);
}
Zos.flush ();
Zos.closeentry ();
} catch (IOException ex) {
Throw ex;
} finally {
if (null! = is) {
Is.close ();
}
}
}
/**
* Create a directory
* @param zos Zip output stream
* @param relativepath relative path
* @throws IOException
*/
private static void Createzipnode (Zipoutputstream zos, String relativepath) throws IOException {
ZipEntry zipentry = new ZipEntry (relativepath);
Zos.putnextentry (ZipEntry);
Zos.closeentry ();
}
/**
* Unzip Zip Package
* @param zipfilepath Zip file path
* @param TargetPath to the location, if null or empty string is the default decompression to the ZIP package in the same directory as the ZIP package under the same name folder
* @throws IOException
*/
public static void Unzip (string zipfilepath, String TargetPath) throws IOException {
ZipFile zipfile = null;
OutputStream OS = null;
InputStream is = null;
String Dirpath;
try {
ZipFile = new ZipFile (Zipfilepath);
if (Ccommon.isnullorempty (TargetPath)) {
Dirpath = zipfilepath.substring (0, Zipfilepath.lastindexof ("."));
} else {
Dirpath = Ccommon.getdirectory (TargetPath);
}
enumeration<?> entries = Zipfile.getentries ();
if (entries! = null) {
while (Entries.hasmoreelements ()) {
ZipEntry entry = (zipentry) entries.nextelement ();
String FilePath = dirpath + File.separator + entry.getname ();
if (Entry.isdirectory ()) {
Ccommon.createfile (FilePath, true);
} else {
File targetfile = Ccommon.createfile (FilePath, false);
OS = new Bufferedoutputstream (new FileOutputStream (targetfile));
if (entry.getsize () > 0) {
is = Zipfile.getinputstream (entry);
byte[] buffer = new byte[4096];
int readlen = 0;
while ((Readlen = is.read (buffer, 0, 4096)) >= 0) {
Os.write (buffer, 0, Readlen);
}
Is.close (); is = null;
Os.flush ();
}
Os.close (); OS = null;
}
}
}
} catch (IOException ex) {
Throw ex;
} finally {
if (is! = null) is.close ();
if (OS! = null) os.close ();
if (ZipFile! = null) zipfile.close ();
}
}
}
Zip compression, File download