zipUtil
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipTool {
/**
* 压缩文件
*
* @param zipFileName
* 保存的压缩包文件路径
* @param inputFile
* 需要压缩的文件夹或者文件路径
* @throws Exception
*/
public static void zip(String zipFileName, String filePath, boolean isDelete)
throws Exception {
zip(zipFileName, new File(filePath), isDelete);
}
@SuppressWarnings("resource")
public static void zip(String zipFileName, File inputFile, boolean isDelete)
throws Exception {
FileUtils.createDictionary(zipFileName);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
if (!inputFile.exists()) {
throw new FileNotFoundException("在指定路径未找到需要压缩的文件!");
}
zip(out, inputFile, "", isDelete);// 递归压缩方法
System.out.println("Zip Done.");
out.close();
}
/**
* 递归压缩方法
*
* @param out
* 压缩包输出流
* @param f
* 需要压缩的文件
* @param base
* 压缩的路径
* @throws Exception
*/
private static void zip(ZipOutputStream out, File f, String base,
boolean isDelete) throws Exception {
String path = f.getPath();
System.out.println("Zipping : " + f.getPath()); // 记录日志,开始压缩
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(), isDelete);
}
} else { // 如果是文件,则压缩
out.putNextEntry(new ZipEntry(base)); // 生成下一个压缩节点
FileInputStream in = new FileInputStream(f); // 读取文件内容
int b;
while ((b = in.read()) != -1) {
out.write(b); // 写入到压缩包
}
in.close();
}
if (isDelete) {
FileUtils.deleteFile(new File(path));
}
}
public static void unZip(String zipFilePath, String fileSavePath,
boolean isDelete) throws Exception {
/*
* if (!fileSavePath.endsWith("//")) { fileSavePath += "//"; }
*/
File file = new File(zipFilePath);
File savePath = new File(fileSavePath);
// 验证待解压文件是否存在
if (!file.exists()) {
throw new FileNotFoundException("在指定路径未找到ZIP压缩文件!");
}
// 创建解压缩目录
if (!savePath.exists()) {
savePath.mkdirs();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
FileOutputStream fos = null;
ZipEntry entry = null;
int b;
while ((entry = zis.getNextEntry()) != null) {
FileUtils.createDictionary(fileSavePath + entry.getName());
file = new File(fileSavePath + entry.getName());
if (entry.isDirectory()) {
// 目录
System.out.println("create dir : " + file.getPath());
file.mkdirs();
} else {
// 文件
System.out.println("create file: " + file.getPath());
/*
* RandomAccessFile rf = new RandomAccessFile(file, "rwd");
* rf.seek(0); while ((b = zis.read()) != -1) { //fos.write(b);
* rf.write(b); } rf.close();
*/
fos = new FileOutputStream(file);
while ((b = zis.read()) != -1) {
fos.write(b);
}
fos.close();
}
}
zis.close();
if (isDelete) {
new File(zipFilePath).delete();
}
System.out.println("unZip Done.");
}
public void randomAccessFile(String file) throws Exception {
RandomAccessFile f = new RandomAccessFile(file, "rw");
System.out.println("File.lelngth:" + (f.length()) + "B");
System.out.println("File PointPosition:" + f.getFilePointer());
f.seek(f.length());
f.writeBoolean(true);
f.writeBoolean(false);
f.writeChar('a');
f.writeChars("hello!");
System.out.println("File Length;" + (f.length()) + "B");
f.seek(0);
System.out.println(f.readBoolean());
System.out.println(f.readBoolean());
System.out.println(f.readLine());
f.close();
}
public void chinesewrit(String toAppend) {
try {
// 写入
int i = 0;
String record = new String();
String toCn = null;
// 处理中文问题
toCn = new String(toAppend.getBytes("GBK"), "ISO8859_1");
RandomAccessFile rf = new RandomAccessFile("c://aaa.txt", "rw");
rf.seek(rf.length());
rf.writeBytes(toCn + "/n");
rf.close();
// 读取
RandomAccessFile rf2 = new RandomAccessFile("c:/aaa.txt", "r");
String outCn = null;
while ((record = rf2.readLine()) != null) {
i++;
// 处理中文问题
outCn = new String(record.getBytes("ISO8859_1"), "GBK");
System.out.println("Line " + i + ":" + outCn);
}
rf2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上一篇: 华为Mate40 Pro+不慎从21楼跌落:最后仅损坏屏幕、全程没断电
下一篇: 一片黑暗
推荐阅读