Java compressed/uncompressed ZIP format file

Source: Internet
Author: User

1package Com.resoft.util;

3import Java.io.BufferedOutputStream;

4import Java.io.File;

5import Java.io.FileInputStream;

6import java.io.FileNotFoundException;

7import Java.io.FileOutputStream;

8import java.io.IOException;

9import Java.io.InputStream;

10import Java.io.OutputStream;

11import java.util.Enumeration;

13import Org.apache.tools.zip.ZipEntry;

14import Org.apache.tools.zip.ZipFile;

15import Org.apache.tools.zip.ZipOutputStream;

17/** *//**
* Compressed/uncompressed ZIP package processing class
*
* @author Yayagepei
* @date 2008-8-25
*/

23public class Ziputil {

/** *//**
* Compression
*
* @param zipfilename
* Compression generated ZIP package file name--with path, if null or empty by default production of compressed file names by file name
* @param relativepath
* Relative path, default is empty
* @param directory
* Absolute path to file or directory
* @throws FileNotFoundException
* @throws IOException
* @author Yayagepei
* @date 2008-8-26
*/
public static void Zip (String zipfilename, String relativepath,
String directory) throws FileNotFoundException, IOException {
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
* @author yayagepei
* @date 2008-8-26
*/
private static void zip (Zipoutputstream Zos, String relativepath,
string absolutpath) throws IOException {
File File = new file (absolutpa TH);
if (file.isdirectory ()) {
file[] files = file.listfiles ();
for (int i = 0; i < files.length; i++) {
Fi Le 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 File
*
* @param zos
* Compressed output stream
* @param file
* Files Object
* @param relativepath
* Relative path
* @throws ioexception
* @author yayagepei
* @date 2008-8-26
*/
private static void ZipFile (ZipOut Putstream zos, File file,
String relativepath) throws IOException {
ZipEntry entry = new ZipEntry (RelativePath + F Ile.getname ());
Zos.putnextentry (entry);
InputStream is = null;
try {
is = new FileInputStream (file),
int buffersize = 2 <<;
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
* @author Yayagepei
* @date 2008-8-26
*/
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
* Unzip to the location, if null or empty string is the default decompression to the same directory as the ZIP package with the same folder as the ZIP package
* @throws IOException
* @author Yayagepei
* @date 2008-9-28
*/
public static void Unzip (string zipfilepath, String TargetPath)
Throws IOException {
OutputStream OS = null;
InputStream is = null;
ZipFile zipfile = null;
try {
ZipFile = new ZipFile (Zipfilepath);
String directorypath = "";
if (null = = TargetPath | | "". Equals (TargetPath)) {
DirectoryPath = zipfilepath.substring (0, Zipfilepath
. LastIndexOf ("."));
} else {
DirectoryPath = TargetPath;
}
Enumeration entryenum = Zipfile.getentries ();
if (null! = Entryenum) {
ZipEntry zipentry = null;
while (Entryenum.hasmoreelements ()) {
ZipEntry = (zipentry) entryenum.nextelement ();
if (Zipentry.isdirectory ()) {
DirectoryPath = DirectoryPath + file.separator
+ Zipentry.getname ();
System.out.println (DirectoryPath);
Continue
}
if (zipentry.getsize () > 0) {
File
File targetfile = Fileutil.buildfile (directorypath
+ File.separator + zipentry.getname (), false);
OS = new Bufferedoutputstream (New FileOutputStream (
TargetFile));
is = Zipfile.getinputstream (zipentry);
byte[] buffer = new byte[4096];
int readlen = 0;
while ((Readlen = is.read (buffer, 0, 4096)) >= 0) {
Os.write (buffer, 0, Readlen);
}

Os.flush ();
Os.close ();
} else {
Empty directory
Fileutil.buildfile (DirectoryPath + file.separator
+ Zipentry.getname (), true);
}
}
}
} catch (IOException ex) {
Throw ex;
} finally {
if (null! = ZipFile) {
ZipFile = null;
}
if (null! = is) {
Is.close ();
}
if (null! = OS) {
Os.close ();
}
}
}

230}

A way to add a fileutil that you wrote yourself.
/**

* Production files if the path where the file is located does not exist then the path is generated

*

* @param fileName

* File name with path

* @param isdirectory is the path

* @return

* @author Yayagepei

* @date 2008-8-27

*/

public static File BuildFile (String fileName, Boolean isdirectory) {

File target = new file (fileName);

if (isdirectory) {

Target.mkdirs ();

} else {

if (!target.getparentfile (). exists ()) {

Target.getparentfile (). Mkdirs ();

target = new File (Target.getabsolutepath ());

}

}

return target;

}

Http://www.cnblogs.com/yayagepei/articles/1309789.html

Java compressed/uncompressed ZIP format file

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.