apache ant停止zip解緊縮操作示例分享。本站提示廣大學習愛好者:(apache ant停止zip解緊縮操作示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是apache ant停止zip解緊縮操作示例分享正文
須要導入ant.jar包,apache網站(http://ant.apache.org/bindownload.cgi)下載便可。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;
import com.xyq.io.util.CloseIoUtil;
public class ZipUtil {
private static final String ENCODE = "UTF-8";
public static void zip(String inputFilePath, String zipFileName) {
File inputFile = new File(inputFilePath);
if (!inputFile.exists())
throw new RuntimeException("原始文件不存在!!!");
File basetarZipFile = new File(zipFileName).getParentFile();
if (!basetarZipFile.exists() && !basetarZipFile.mkdirs())
throw new RuntimeException("目的文件沒法創立!!!");
BufferedOutputStream bos = null;
FileOutputStream out = null;
ZipOutputStream zOut = null;
try {
// 創立文件輸入對象out,提醒:留意中文支撐
out = new FileOutputStream(new String(zipFileName.getBytes(ENCODE)));
bos = new BufferedOutputStream(out);
// 將文件輸出ZIP輸入流接起來
zOut = new ZipOutputStream(bos);
zip(zOut, inputFile, inputFile.getName());
CloseIoUtil.closeAll(zOut, bos, out);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void zip(ZipOutputStream zOut, File file, String base) {
try {
// 假如文件句柄是目次
if (file.isDirectory()) {
// 獲得目次下的文件
File[] listFiles = file.listFiles();
// 樹立ZIP條目
zOut.putNextEntry(new ZipEntry(base + "/"));
base = (base.length() == 0 ? "" : base + "/");
if (listFiles != null && listFiles.length > 0)
// 遍歷目次下文件
for (File f : listFiles)
// 遞歸進入本辦法
zip(zOut, f, base + f.getName());
}
// 假如文件句柄是文件
else {
if (base == "") {
base = file.getName();
}
// 填入文件句柄
zOut.putNextEntry(new ZipEntry(base));
// 開端緊縮
// 從文件入流讀,寫入ZIP 出流
writeFile(zOut, file);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void writeFile(ZipOutputStream zOut, File file)
throws IOException {
FileInputStream in = null;
BufferedInputStream bis = null;
in = new FileInputStream(file);
bis = new BufferedInputStream(in);
int len = 0;
byte[] buff = new byte[2048];
while ((len = bis.read(buff)) != -1)
zOut.write(buff, 0, len);
zOut.flush();
CloseIoUtil.closeAll(bis, in);
}
/****
* 解壓
*
* @param zipPath
* zip文件途徑
* @param destinationPath
* 解壓的目標所在
* @param ecode
* 文件名的編碼字符集
*/
public static void unZip(String zipPath, String destinationPath) {
File zipFile = new File(zipPath);
if (!zipFile.exists())
throw new RuntimeException("zip file " + zipPath
+ " does not exist.");
Project proj = new Project();
Expand expand = new Expand();
expand.setProject(proj);
expand.setTaskType("unzip");
expand.setTaskName("unzip");
expand.setSrc(zipFile);
expand.setDest(new File(destinationPath));
expand.setEncoding(ENCODE);
expand.execute();
System.out.println("unzip done!!!");
}
public static void main(String[] args) {
String dir = new String("F:\\我的備份\\文檔\\MyEclipse+9.0正式版破解與激活(親測可用)");
dir = new String("F:/111.JPG");
zip(dir, "f:/BZBXB/zipant.zip");
unZip("f:/BZBXB/zipant.zip", "f:/XX/xx/");
}
}