public class Zip {
public Zip() {
}
/**
* 壓縮整個(gè)目錄
* @param inputFileName
* @param fileDir
* @throws HsException
* @throws Exception
*/
public void zip(String zipDirFileName,String fileDir) throws HsException{
try {
zip(zipDirFileName, new File(fileDir));
} catch (Exception e) {
throw new HsException(HsErrorMsg.ERR_DEFAULT,"壓縮文件夾出錯(cuò)!",e);
}
}
/**
* 壓縮整個(gè)目錄
* @param zipFileName
* @param inputFile
* @throws Exception
*/
private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("zip done");
out.close();
}
private void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
* @param zipFileName為需要解壓的zip文件
* @param extPlace為解壓后文件的存放路徑
* @param qName 解壓后的名字前面加上前綴
* return 所有文件
* @throws HsException
*/
public String[] UnZip(String zipFileName, String extPlace,String qName) throws HsException {
String files = "";
try{
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
String names[] = entryName.split("/");
int length = names.length;
String path = extPlace;
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += names[v] + "/";
new File(path).mkdir();
}else { // 最后一個(gè)
if (entryName.endsWith("/")) { // 為目錄,則創(chuàng)建文件夾
new File(extPlace + entryName).mkdir();
}else {
InputStream in = zipFile.getInputStream(zipEntry);
OutputStream os = new FileOutputStream(new File(extPlace + qName+entryName));
byte[] buf = new byte[1024];
int len;
while ( (len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
files += qName+entryName+"|";
in.close();
os.close();
}
}
}
}
zipFile.close();
return StringUtils.split(files,"|");
}catch(Exception e){
throw new HsException(HsErrorMsg.FILE_ERROR,"文件解壓縮出錯(cuò),文件:"+zipFileName);
}
}
/**
*
* @param zipFileName為需要解壓的zip文件
* @param extPlace為解壓后文件的存放路徑
* @throws Exception
*/
public void UnZip(String zipFileName, String extPlace) throws HsException {
try{
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
String names[] = entryName.split("/");
int length = names.length;
String path = extPlace;
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += names[v] + "/";
new File(path).mkdir();
}else { // 最后一個(gè)
if (entryName.endsWith("/")) { // 為目錄,則創(chuàng)建文件夾
new File(extPlace + entryName).mkdir();
}else {
InputStream in = zipFile.getInputStream(zipEntry);
OutputStream os = new FileOutputStream(new File(extPlace + entryName));
byte[] buf = new byte[1024];
int len;
while ( (len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
in.close();
os.close();
}
}
}
}
zipFile.close();
}catch(Exception e){
throw new HsException(HsErrorMsg.FILE_ERROR,"文件解壓縮出錯(cuò),文件:"+zipFileName);
}
}
public static void main(String args[]){
Zip zip = new Zip();
try {
//zip.zip("D:\\ECLIPSETEST\\bots\\tamcx\\app/bots/infoDown/10001/20080920/1.zip",
// "D:\\ECLIPSETEST\\bots\\tamcx\\app/bots/infoDown/10001/20080920/zip/");
} catch (Exception e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
}
}
}  public class ZipReadWrite {
public ZipReadWrite() {
}
/**
* 功能:解壓zip文件,解壓文件存放在本壓縮文件所在目錄下
*
* @param zipFileName
*/
public void ZipUnPack(String zipFileName) throws HsException {
FileInputStream fin = null;
ZipInputStream zin = null;
DataInputStream din = null;
FileOutputStream fout = null;
try {
String filePath = getFileDir(zipFileName);
if(filePath == null){
throw new HsException(HsErrorMsg.ERR_DEFAULT,"文件路徑名不合法!");
}
fin = new FileInputStream(zipFileName);
zin = new ZipInputStream(fin);
din = new DataInputStream(zin);
ZipEntry ze = null;
ze = zin.getNextEntry();
File fi = null;
while (ze != null) {
String enName = ze.getName();
// int length = (int) ze.getSize();
// byte[] bt = new byte[length];
// din.readFully(bt);
fi = new File(filePath + enName);
fout = new FileOutputStream(fi);
IOUtils.copy(new DataInputStream(zin), fout);
// fout.write(bt);
fout.flush();
fout.close();
ze = zin.getNextEntry();
}
} catch (Exception e) {
e.printStackTrace();
throw new HsException(HsErrorMsg.ERR_DEFAULT, "文件解壓縮出現(xiàn)異常!");
} finally {
try {
if (fin != null) {
fin.close();
}
if (zin != null) {
zin.close();
}
if (din != null) {
din.close();
}
} catch (IOException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
throw new HsException(HsErrorMsg.ERR_DEFAULT, "文件解壓縮關(guān)閉出現(xiàn)異常!");
}
}
}
/**
* 功能:解壓zip文件,解壓文件存放在指定目錄下
*
* @param zipFileName
* @param saveDir
* 示例:e:/test/test2/ 或e:/test/test2
* @throws HsException
*/
public void ZipUnPack(String zipFileName, String saveDir)
throws HsException {
FileInputStream fin = null;
ZipInputStream zin = null;
DataInputStream din = null;
FileOutputStream fout = null;
try {
String filePath = saveDir;
String lastChar = saveDir.substring(saveDir.length() - 1, saveDir
.length());
if (!(lastChar.equals("/") || lastChar.equals("\\"))) {
filePath = saveDir + "/";
}
File file = new File(saveDir);
if (!file.isDirectory()) {
file.mkdirs();
}
fin = new FileInputStream(zipFileName);
zin = new ZipInputStream(fin);
din = new DataInputStream(zin);
ZipEntry ze = null;
ze = zin.getNextEntry();
File fi = null;
while (ze != null) {
String enName = ze.getName();
// int length = (int) ze.getSize();
// byte[] bt = new byte[length];
// din.readFully(bt);
fi = new File(filePath + enName);
fout = new FileOutputStream(fi);
IOUtils.copy(new DataInputStream(zin), fout);
// fout.write(bt);
fout.flush();
fout.close();
ze = zin.getNextEntry();
}
} catch (Exception e) {
e.printStackTrace();
throw new HsException(HsErrorMsg.ERR_DEFAULT, "文件解壓縮出現(xiàn)異常!");
} finally {
try {
if (fin != null) {
fin.close();
}
if (zin != null) {
zin.close();
}
if (din != null) {
din.close();
}
} catch (IOException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
throw new HsException(HsErrorMsg.ERR_DEFAULT, "文件解壓縮關(guān)閉出現(xiàn)異常!");
}
}
}
/**
* 功能:得到文件路徑的目錄名
*
* @param filePath d:\dir\filename
* @return d:\dir
*/
public String getFileDir(String filePath) {
String dir = "";
int i = filePath.lastIndexOf("\\");
int j = filePath.lastIndexOf("/");
if (i > 0&&j==-1) {
dir = filePath.substring(0, i + 1);
} else if(j>0&&i==-1) {
dir = filePath.substring(0, j + 1);
}else if(i>j){
dir = filePath.substring(0, i + 1);
}else if(j>i){
dir = filePath.substring(0, j + 1);
}
return dir;
}
public void ZipPack(String fileDir) throws HsException {
FileOutputStream fout = null;
DataOutputStream dout = null;
FileInputStream fin2 = null;
DataInputStream din2 = null;
try {
//去除路徑最后的‘/’或者‘\’
String lastChar = fileDir.substring(fileDir.length()-1);
if (lastChar.equals("/") || lastChar.equals("\\")) {
fileDir = fileDir.substring(0,fileDir.length()-1);
}
int last = fileDir.lastIndexOf("/");
String dirName = null;
if(last>=0){
dirName = fileDir.substring(last+1);
}else{
last = fileDir.lastIndexOf("\\");
if(last>=0){
dirName = fileDir.substring(last+1);
}else {
throw new HsException(HsErrorMsg.ERR_DEFAULT,"文件目錄不合法!");
}
}
//得到該目錄的同名目錄
int lastIndex = fileDir.lastIndexOf("/");
if(lastIndex<0){
lastIndex = fileDir.lastIndexOf("\\");
}
String creatDir = fileDir.substring(0,lastIndex);
fout = new FileOutputStream(creatDir+"/"+dirName+".zip");
File file = new File(fileDir);
File[] files = file.listFiles();
ZipOutputStream zout = new ZipOutputStream(fout);
zout.setMethod(ZipOutputStream.DEFLATED);
dout = new DataOutputStream(zout);
for (int i = 0; i < files.length; i++) {
File fi2 = files[i];
fin2 = new FileInputStream(fi2);
zout.putNextEntry(new ZipEntry(fi2.getName()));
byte[] bt = new byte[fin2.available()];
din2 = new DataInputStream(fin2);
din2.readFully(bt);
dout.write(bt);
fin2.close();
din2.close();
}
dout.flush();
dout.close();
zout.close();
} catch (Exception e) {
e.printStackTrace();
throw new HsException(HsErrorMsg.ERR_DEFAULT, "壓縮zip包時(shí)出錯(cuò)!");
}
finally {
try {
if (fout != null) {
fout.close();
}
if (dout != null) {
dout.close();
}
} catch (IOException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
throw new HsException(HsErrorMsg.ERR_DEFAULT, "文件壓縮關(guān)閉出現(xiàn)異常!");
}
}
}
} public class RaR {
/*
* cmd 壓縮與解壓縮命令
*/
private static String rarCmd = "C:\\Program Files\\WinRAR\\Rar.exe a ";
private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x ";
/**
* 將1個(gè)文件壓縮成RAR格式
* rarName 壓縮后的壓縮文件名(不包含后綴)
* fileName 需要壓縮的文件名(必須包含路徑)
* destDir 壓縮后的壓縮文件存放路徑
*/
public static void RARFile(String rarName, String fileName, String destDir) {
rarCmd += destDir + rarName + ".rar " + fileName;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(rarCmd);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 將1個(gè)RAR文件解壓
* rarFileName 需要解壓的RAR文件(必須包含路徑信息以及后綴)
* destDir 解壓后的文件放置目錄
*/
public static void unRARFile(String rarFileName, String destDir) {
unrarCmd += rarFileName + " " + destDir;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(unrarCmd);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}  import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipUtils
{
/**
* 解壓縮zip文件
* @param sZipPathFile
* @param sDestPath
* @return
* @throws IOException
*/
public static List<String> extract(String sZipPathFile, String sDestPath) throws IOException
{
List<String> allFileName = new ArrayList<String>();
//先指定壓縮檔的位置和檔名,建立FileInputStream對(duì)象
FileInputStream fins = new FileInputStream(sZipPathFile);
//將fins傳入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null)
{
File zfile = new File(sDestPath + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory())
{
if (!zfile.exists())
{
zfile.mkdirs();
}
zins.closeEntry();
} else
{
if (!fpath.exists())
{
fpath.mkdirs();
}
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
allFileName.add(zfile.getAbsolutePath());
while ((i = zins.read(ch)) != -1)
{
fouts.write(ch, 0, i);
}
zins.closeEntry();
fouts.close();
}
}
fins.close();
zins.close();
return allFileName;
}
public static void main(String[] args)
{
List<String> a = null;
try
{
a = ZipUtils.extract("d:\\src.zip", "c:\\");
} catch (IOException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println(a.size());
}
}
|