You need to import the ant.jar package and download it on the apache website (http://ant.apache.org/bindownload.cgi).
The code copy is as follows:
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("The original file does not exist!!!");
File basetarZipFile = new File(zipFileName).getParentFile();
if (!basetarZipFile.exists() && !basetarZipFile.mkdirs())
throw new RuntimeException("The target file cannot be created!!!");
BufferedOutputStream bos = null;
FileOutputStream out = null;
ZipOutputStream zOut = null;
try {
// Create file output object out, prompt: Pay attention to Chinese support
out = new FileOutputStream(new String(zipFileName.getBytes(ENCODE)));
bos = new BufferedOutputStream(out);
// File output streaming is connected
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 the file handle is a directory
if (file.isDirectory()) {
// Get the file in the directory
File[] listFiles = file.listFiles();
// Create a ZIP entry
zOut.putNextEntry(new ZipEntry(base + "/"));
base = (base.length() == 0 ? "" : base + "/");
if (listFiles != null && listFiles.length > 0)
//Travel the files in the directory
for (File f : listFiles)
// Recursively enter this method
zip(zOut, f, base + f.getName());
}
// If the file handle is a file
else {
if (base == "") {
base = file.getName();
}
// Fill in the file handle
zOut.putNextEntry(new ZipEntry(base));
// Start compression
// Read from file inflow, write to ZIP outflow
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);
}
/***
* Decompression
*
* @param zipPath
* zip file path
* @param destinationPath
* Decompression destination
* @param ecode
* Encoded character set of filenames
*/
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://My backup//Document//MyEclipse+9.0 official version cracking and activation (professional test available)");
dir = new String("F:/111.JPG");
zip(dir, "f:/BZBXB/zipant.zip");
unZip("f:/BZBXB/zipant.zip", "f:/XX/xx/");
}
}