Java Packaging Compressed files

Source: Internet
Author: User

Package com.it.simple.util;

Import Java.io.BufferedOutputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.io.Writer;
Import java.util.Enumeration;

Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
Import Org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
Import Org.apache.commons.compress.archivers.zip.ZipFile;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.commons.io.IOUtils;
Import Org.apache.commons.lang.StringUtils;

/***
* Write the compressed file out of the page Web download
* @ Rack Bag Commons-compress-1.8.jar
*
*/
public class zipfileutils{

public static void Main (string[] args) throws FileNotFoundException, Exception {
/*zipfileutils main = new Zipfileutils ("C:/test1.zip");
Main.createzipout ();
Main.packtoolfiles ("d:/444", "444");
Main.closezipout (); */

/*main = new Main ("C:/test2.zip");
Main.createzipout ();
Main.packtoolfiles ("C:/test", "Test");
Main.closezipout ();
Main.unziptofolder ("C:/test1.zip", "C:/test1");
Main.unziptofolder ("C:/test2.zip", "C:/test2");
*/
}
Private outputstream out = null;
Private Bufferedoutputstream BOS = NULL;
Private Ziparchiveoutputstream Zaos = null;
Private String zipfilename = null;

Public Zipfileutils (String zipname,httpservletresponse response) {
This.zipfilename = Zipname;
try{
String fileName = "File package Download _" +system.currenttimemillis () + ". zip";
Response.setheader ("Content-disposition", "attachment;filename=\" "
+ New String (Filename.getbytes ("gb2312"), "iso8859-1") + "\" ");
Response.setcontenttype ("Application/octet-stream");
Response.setcontenttype ("Multipart/form-data");
out = Response.getoutputstream ();
}catch (Exception e) {
}

}

public void Createzipout () throws FileNotFoundException, IOException {
File F = new file (zipfilename);
out = new FileOutputStream (f);
BOS = new Bufferedoutputstream (out);
Zaos = new Ziparchiveoutputstream (BOS);
Zaos.setencoding ("GBK");
}

public void Closezipout () throws Exception {
Zaos.flush ();
Zaos.close ();

Bos.flush ();
Bos.close ();

Out.flush ();
Out.close ();

}

/**
* Package a directory into a directory in a zip file
* @param dirpath Directory Absolute Address
* @param directory in PathName zip
*/
public void Packtoolfiles (string dirpath, String pathName) throws FileNotFoundException, IOException {
if (Stringutils.isnotempty (PathName)) {
PathName = PathName + file.separator;
}
Packtoolfiles (Zaos, Dirpath, pathName);
}

/**
* Package a directory into a specified zip file
* @param dirpath Directory Absolute Address
* @param pathName zip file abstract Address
*/
public void Packtoolfiles (Ziparchiveoutputstream zaos, String Dirpath, String pathName) throws FileNotFoundException, IOException {

Bytearrayoutputstream Tempbaos = new Bytearrayoutputstream ();
Bufferedoutputstream Tempbos = new Bufferedoutputstream (Tempbaos);

File dir = new file (Dirpath);
Returns the file under this absolute path
file[] files = dir.listfiles ();
if (files = = NULL | | Files.length < 1) {
Return
}
for (int i = 0; i < files.length; i++) {
Determine if this file is a folder
if (Files[i].isdirectory ()) {
Packtoolfiles (Zaos, Files[i].getabsolutepath (), PathName + files[i].getname () + file.separator);
} else {
Zaos.putarchiveentry (New Ziparchiveentry (PathName + files[i].getname ()));
Ioutils.copy (New FileInputStream (Files[i].getabsolutepath ()), Zaos);
Zaos.closearchiveentry ();
}

}

Tempbaos.flush ();
Tempbaos.close ();

Tempbos.flush ();
Tempbos.close ();
}

/**
* Unzip a zip file into a specified directory
* @param zipfilename zip file abstract Address
* @param outputdir Directory Absolute Address
*/
public static void Unziptofolder (String zipfilename, String outputdir) throws IOException {
File ZipFile = new file (zipfilename);
if (zipfile.exists ()) {
OutputDir = OutputDir + file.separator;
Fileutils.forcemkdir (New File (OutputDir));

ZipFile ZF = new ZipFile (ZipFile, "GBK");
Enumeration Ziparchiveentrys = Zf.getentries ();
while (Ziparchiveentrys.hasmoreelements ()) {
Ziparchiveentry ziparchiveentry = (ziparchiveentry) ziparchiveentrys.nextelement ();
if (Ziparchiveentry.isdirectory ()) {
Fileutils.forcemkdir (New File (OutputDir + ziparchiveentry.getname () + file.separator));
} else {
Ioutils.copy (Zf.getinputstream (Ziparchiveentry), Fileutils.openoutputstream (new File (OutputDir + Ziparchiveentry.getname ())));
}
}
} else {
throw new IOException ("The specified decompression file does not exist: \ t" + zipfilename);
}
}
}

Note: If it is a Web project, OutputStream flows in

Java Packaging Compressed files

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.