最近由于工作需要,研究了一下util.zip的api函数!看网上的好多教程讲得都不是很好!,所以给自己bakup一下。
咱们用zip来解压缩文件主要就用其中的ZipInputStream,ZipOutputStream,ZipFile,ZipEntry就ok了。
在这里ZipInputStream,ZipOutputStream都很好理解,前者用来读取zip文件,后者用来压缩。关于ZipEntry可能是初学者最容易犯迷惑的类。其实我们可以把ZipEntry理解成压缩文件中的一个目录和一个文件,给一个用来实现解两层zip文件和压两层zip文件的例子,你看明白这个例子的话,你就彻底明白java的zip了!
用法:1, readZipFile("FTP-HTTP.zip");需要你修改一下,给一个有两层zip的zip文件(也就是zip中还有zip文件)
建议你先直接运行,看一看运行出来的结果,压缩好的xuqk.zip的结构,然后研究研究代码,你就会了。。。口才不好,表达不出来,以后我会锻炼的,现在只能用程序说话了!!!!
package zip;
import java.util.zip.*;
import java.io.*;
public class ReadWriteZipFile {
/**
* @param args
*/
public static void main(String[] args) {
readZipFile("FTP-HTTP.zip");
writeZipFile("xuqk.zip", "");
}
public static void readZipFile(String fileName) {
try {
InputStream zipFileName = new FileInputStream(new File(fileName));
ZipInputStream zipInputStream = new ZipInputStream(zipFileName);
ZipFile zipFile = new ZipFile(fileName);
ZipEntry z;
echo("------Directory------start------");
while ((z = zipInputStream.getNextEntry()) != null) {
String entryName = z.getName();
echo(" " + entryName + "------"
+ (z.isDirectory() ? "Directory" : "File"));
// print the file content
// echo("---------------------------------------------");
// int x;
// while ((x = zipInputStream.read()) != -1)
// System.out.write(x);
// echo("---------------------------------------------");
if (entryName.endsWith(".zip")) {
ZipInputStream subZipInputStream = new ZipInputStream(
zipFile.getInputStream(z));
ZipEntry subZ;
echo(" ---SubDirectory------start------");
while ((subZ = subZipInputStream.getNextEntry()) != null) {
String subEntryName = subZ.getName();
echo(" " + subEntryName + "------"
+ (z.isDirectory() ? "Directory" : "File"));
}
echo(" ---SubDirectory------end------");
}
}
echo("------Directory------end------");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeZipFile(String zipFileName, String inputFile) {
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
// for put directory
out.putNextEntry(new ZipEntry("test1/rrr.txt"));
String ss = "This is My test for zip file/nOh Thank you!!";
byte[] bs = ss.getBytes();
out.write(bs);
out.putNextEntry(new ZipEntry("test2/fff"));
out.putNextEntry(new ZipEntry("test3/fff"));
out.putNextEntry(new ZipEntry("test4/ccc"));
out.putNextEntry(new ZipEntry("test5/CBR.zip"));
ZipOutputStream subOut = new ZipOutputStream(out);
subOut.putNextEntry(new ZipEntry("haha.txt"));
String s = "This is My test for zip file/nOh Thank you!";
byte[] b = s.getBytes();
subOut.write(b);
System.out.println("zip done");
subOut.close();
out.close();
} catch (Exception e) {
}
}
public static void zip(ZipOutputStream out, File f, String base)
throws Exception {
System.out.println("Zipping " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new 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 ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}
}
private static void echo(String msg) {
System.out.println(msg);
}
}