package TestLinkPackage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZIPUtil
{
private File workspaceFile;
private File dirFile;
private File jmeterDirectory;
private File jmeterZip;
private String workspacePath;
private String jmeterDirectoryPath;
private String jmeterZipPath;
public void UnZipFile()
throws IOException
{
this.workspaceFile = new File("");
this.workspacePath = this.workspaceFile.getAbsolutePath();
this.dirFile = new File(this.workspacePath);
File[] files = this.dirFile.listFiles();
this.jmeterDirectoryPath = (this.workspacePath + "\\JmeterFolder");
this.jmeterDirectory = new File(this.jmeterDirectoryPath);
File[] zipFiles = this.jmeterDirectory.listFiles();
this.jmeterZipPath = (this.jmeterDirectoryPath + "\\apache-jmeter-4.0.zip");
this.jmeterZip = new File(this.jmeterZipPath);
ZipFile zipFile = new ZipFile(this.jmeterZip, Charset.forName("GBK"));
if (files.length != 0)
{
File[] arrayOfFile1;
int j = (arrayOfFile1 = files).length;
for (int i = 0; i < j; i++)
{
File file = arrayOfFile1[i];
if (file.getName().equals("JmeterFolder")) {
if (zipFiles.length != 0)
{
File[] arrayOfFile2;
int m = (arrayOfFile2 = zipFiles).length;
for (int k = 0; k < m; k++)
{
File jmeterZipFile = arrayOfFile2[k];
if (jmeterZipFile.getName().equals("apache-jmeter-4.0.zip"))
{
for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();)
{
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zipFile.getInputStream(entry);
String outPath = (this.jmeterDirectoryPath + "\\" + zipEntryName).replaceAll("\\*", "/");
File jmeterFile = new File(outPath.substring(0, outPath.lastIndexOf(‘/‘)));
if (!jmeterFile.exists()) {
jmeterFile.mkdirs();
}
if (!new File(outPath).isDirectory())
{
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[‘?‘];
int len;
while ((len = in.read(buf1)) > 0)
{
int len;
out.write(buf1, 0, len);
}
in.close();
out.close();
}
}
System.out.println("******************解压完毕********************");
}
else
{
System.out.println("apache-jmeter-4.0.zip文件不存在");
}
}
}
else
{
System.out.println("JmeterFolder文件夹为空");
}
}
}
}
else
{
System.out.println("文件夹内容为空");
}
}
}
原文:https://www.cnblogs.com/xxsl/p/11355523.html