📄 ziputil.java
字号:
/**
*
*/
package javazip.util;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.*;
import java.io.*;
import javazip.base.*;
import javazip.base.FileInfo;
/**
* 实现zip压缩的类,继承自CompressUtil
*
* @author dragon
*
*/
public class ZipUtil extends CompressUtil {
ZipOutputStream out;
String comment = "";
static final int BUFFER = 2048;
/**
* 构造函数
*
*/
public void ZipUtil() {
}
/**
* 实现zip的压缩
*
* @see javazip.util.CompressUtil#compressFile(java.util.ArrayList)
*/
public void compressFile(ArrayList fileList, String target) {
try {
out = new ZipOutputStream(new FileOutputStream(target));
for (int i = 0; i < fileList.size(); i++) {
File temp = (File) fileList.get(i);
zip(out, temp, "");
}
out.closeEntry();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
*
*/
public void compressFile(File list[], String target) {
try {
out = new ZipOutputStream(new FileOutputStream(target));
for (int i = 0; i < list.length; i++) {
File temp = (File) list[i];
zip(out, temp, "");
}
out.closeEntry();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 压缩文件和文件夹
*
* @param out
* @param f
* @param target
* @throws Exception
*/
private void zip(ZipOutputStream out, File f, String base) throws Exception {
out.setComment(comment);
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 + f.getName()));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
* 实现zip的解压缩
*
* @see javazip.util.CompressUtil#extractFile(java.lang.String)
*/
public void extractFile(String source, String target) {
try {
ZipFile zipFile = new ZipFile(source);
java.util.Enumeration e = zipFile.entries();
ZipEntry zipEntry = null;
createDirectory(target, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
// System.out.println("unziping " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(target + File.separator + name);
f.mkdir();
// System.out.println("创建目录:" + target + File.separator +
// name);
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
if (fileName.indexOf("/") != -1) {
createDirectory(target, fileName.substring(0, fileName
.lastIndexOf("/")));
fileName = fileName.substring(
fileName.lastIndexOf("/") + 1, fileName
.length());
}
File f = new File(target + File.separator
+ zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
byte[] by = new byte[1024];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.close();
in.close();
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
/**
* 实现解压缩的一个子函数
*
* @param directory
* @param subDirectory
*/
private void createDirectory(String directory, String subDirectory) {
String dir[];
File fl = new File(directory);
try {
if (subDirectory == "" && fl.exists() != true)
fl.mkdir();
else if (subDirectory != "") {
dir = subDirectory.replace('\\', '/').split("/");
for (int i = 0; i < dir.length; i++) {
File subFile = new File(directory + File.separator + dir[i]);
if (subFile.exists() == false)
subFile.mkdir();
directory += File.separator + dir[i];
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String args[]) {
ZipUtil zip = new ZipUtil();
Long l = zip.getOriginalLength(new File("F:\\j-jni.zip"));
System.out.println(l.longValue());
ZipUtil zip2=null;
zip2=new ZipUtil();
Long l1=zip2.getOriginalLength(new File("E:\\sfds.zip"));
System.out.println(l1.longValue());
}
/**
* 取得注释
*
* @return
*/
public String getComment() {
return comment;
}
/**
* 设置注释
*
* @param comment
*/
public void setComment(String comment) {
this.comment = comment;
}
/**
*
*/
public Long getOriginalLength(File f) {
long result = 0;
try {
ZipFile file = new ZipFile(f);
if (!f.exists()) {
return null;
} else {
Enumeration jar_files = file.entries();
while (jar_files.hasMoreElements()) {
ZipEntry zipEntry=(ZipEntry)jar_files.nextElement();
result += zipEntry.getSize();
}
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return new Long(result);
}
/**
*
*/
public boolean testFile(File f) {
int count = 0;
try {
ZipEntry zipEntry = null;
ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
if (!f.exists()) {
return false;
} else {
while ((zipEntry = zis.getNextEntry()) != null) {
count++;
}
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if (count > 0)
return true;
else
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -