Java Extract zip file sample _java

Source: Internet
Author: User
Tags deflater extract zip file int size

If you use a Java-included compression toolkit to achieve the ability to extract files to a specified folder, because the JDK provides the zip can only be processed in UTF-8 format, and the Windows system file name is encoded in GBK, so if you extract a zip package containing the Chinese filename, Illegal parameter exception will be reported, so to achieve decompression, we have to Deflateroutputstream.java, Inflaterinputstream.java, Zipconstants.java, Zipentry.java, Zipinputstream.java and Zipoutputstream.java These related classes are modified as follows:
Since J2SE 1.4, the Java compiler no longer supports import into the name of the class, interface, so in the creation of the Java project, you must create a new definition of the package, the package named format generally for the school domain name in reverse order + their own network name, Like Cn.edu.xidian.crytoll.
In the package, create a new Deflateroutputstream class with the following code:

Deflateroutputstream.java:

Copy Code code as follows:

Package cn.edu.xdian.crytoll;

Import Java.io.FilterOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.zip.Deflater;

/**
* This class implements a output stream filter for compressing data in
* the "deflate" compression format. It is also used as the
* Types of compression filters, such as Gzipoutputstream.
*
* @see Deflater
* @version 1.36, 03/13/06
* @author David Connelly
*/
Public
Class Deflateroutputstream extends Filteroutputstream {
/**
* Compressor for this stream.
*/
protected Deflater def;

/**
* Output buffer for writing compressed data.
*/
protected byte[] BUF;

/**
* Indicates that stream has been closed.
*/

Private Boolean closed = false;

/**
* Creates a new output stream with the specified compressor and
* Buffer size.
* @param out the output stream
* @param def the compressor ("Deflater")
* @param size the output buffer size
* @exception illegalargumentexception If size is <= 0
*/
Public Deflateroutputstream (OutputStream out, deflater def, int size) {
Super (out);
if (out = = NULL | | | def = null) {
throw new NullPointerException ();
else if (size <= 0) {
throw new IllegalArgumentException ("Buffer size <= 0");
}
This.def = def;
BUF = new Byte[size];
}

/**
* Creates a new output stream with the specified compressor and
* A default buffer size.
* @param out the output stream
* @param def the compressor ("Deflater")
*/
Public Deflateroutputstream (OutputStream out, Deflater def) {
This is (out, Def, 512);
}

Boolean usesdefaultdeflater = false;

/**
* Creates a new output stream with a default compressor and buffer size.
* @param out the output stream
*/
Public Deflateroutputstream (OutputStream out) {
This (out, new Deflater ());
Usesdefaultdeflater = true;
}

/**
* Writes a byte to the compressed output stream. This method would
* Block until the byte can be written.
* @param b The byte to be written
* @exception IOException If an I/O error has occurred
*/
public void Write (int b) throws IOException {
byte[] buf = new Byte[1];
Buf[0] = (byte) (b & 0xff);
Write (buf, 0, 1);
}

/**
* Writes an array of bytes to the compressed output stream. This
* method would blocks until all the bytes are written.
* @param b The data to be written
* @param off the start offset of the data
* @param len The length of the data
* @exception IOException If an I/O error has occurred
*/
public void Write (byte[] b, int off, int len) throws IOException {
if (def.finished ()) {
throw new IOException ("Write Beyond End of Stream");
}
if (off | len | (off + len) | (B.length-(off + len)) < 0) {
throw new Indexoutofboundsexception ();
else if (len = = 0) {
Return
}
if (!def.finished ()) {
Deflate no more than stride bytes in a time. This avoids
Excess copying in deflatebytes (DEFLATER.C)
int stride = Buf.length;
for (int i = 0; i < Len; i+= Stride) {
Def.setinput (b, off + I, math.min (stride, len-i));
while (!def.needsinput ()) {
Deflate ();
}
}
}
}

/**
* Finishes writing compressed data to the output stream without closing
* the underlying stream. Use the When applying multiple filters
* In succession to the same output stream.
* @exception IOException If an I/O error has occurred
*/
public void Finish () throws IOException {
if (!def.finished ()) {
Def.finish ();
while (!def.finished ()) {
Deflate ();
}
}
}

/**
* Writes remaining compressed data to the output stream and closes the
* Underlying stream.
* @exception IOException If an I/O error has occurred
*/
public void Close () throws IOException {
if (!closed) {
Finish ();
if (Usesdefaultdeflater)
Def.end ();
Out.close ();
closed = true;
}
}

/**
* Writes next block of compressed data to the output stream.
* @throws IOException If an I/O error has occurred
*/
protected void deflate () throws IOException {
int len = def.deflate (buf, 0, buf.length);
if (Len > 0) {
Out.write (buf, 0, Len);
}
}
}

Related Article

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.