/**
* 解压zip文件
* @param zPath 解压文件存放路径
* @param oPath 解压后的存放路径
* @param isNeedDelSrc 解压后是否删除
* @throws Exception
**/
public static void ZDecodes(String zPath, String oPath,boolean isNeedDelSrc) throws Exception{
OutputStream outputStream = null;
InputStream in = null;
try {
File file = new File(oPath); //解压后的存放路径
if (!file.exists()) file.createNewFile();
outputStream = new FileOutputStream(file);
in = new ZDecodes(new FileInputStream(new File(zPath)));//需要解压的文件
byte[] buf = new byte[8192];
while (true) {
int got = in.read(buf);
if (got < 0) break;
outputStream.write(buf, 0, got);
}
outputStream.flush();
} finally {
if (null != outputStream) outputStream.close();
if (null != in) in.close();
}
//解压后是否删除压缩包
if(isNeedDelSrc){
FileUtils.deleteFile(zPath);
}
}
public static void main (String args[]) throws Exception {
String zPath = "D:/XXX.txt.Z";//需要解压的文件
String oPath = "D:/222/XXX.txt";//解压后的存放路径
ZDecodes.ZDecodes(zPath, oPath, true);
}
}
工具类:
import java.io.*;
public class ZDecodes extends FilterInputStream {
/**
* @param is the input stream to decompress
* @exception IOException if the header is malformed
*/
public ZDecodes(InputStream is) throws IOException
{
super(is);
parse_header();
}
byte[] one = new byte[1];
public synchronized int read() throws IOException
{
int b = in.read(one, 0, 1);
if (b == 1)
return (one[0] & 0xff);
else
return -1;
}
// string table stuff
private static final int TBL_CLEAR = 0x100;
private static final int TBL_FIRST = TBL_CLEAR + 1;
private int[] tab_prefix;
private byte[] tab_suffix;
private int[] zeros = new int[256];
private byte[] stack;
// various state
private boolean block_mode;
private int n_bits;
private int maxbits;
private int maxmaxcode;
private int maxcode;
private int bitmask;
private int oldcode;
private byte finchar;
private int stackp;
private int free_ent;
// input buffer
private byte[] data = new byte[10000];
private int bit_pos = 0, end