📄 jzjfile.java
字号:
package org.jr.jzj;
/**
* <p>Copyright: Copyright (c) 2002-2003</p>
* <p>Company: JavaResearch(http://www.javaresearch.org)</p>
* <p>最后更新日期:2003年1月9日
* @author Cherami,Barney,Brain
* @version 0.8
*/
import java.io.*;
import java.util.*;
import java.util.prefs.*;
import java.util.zip.*;
import org.jr.io.*;
/**
* 压缩文件工具类。
* 此类完成一般的新建、增加、导出、删除操作以及完成对压缩文件的内容的解析。
*/
public class JZJFile {
private static JZJLogger logger = new JZJLogger(JZJFile.class);
Preferences preference = JZipJar.preference;
private boolean isScaned = false;
private boolean isChanged = false;
private File selfFile;
private static File workDirectory = new File(".");
private static boolean haveSetWorkDirectory = false;
private ArrayList entries = new ArrayList();
private ArrayList entryNames = new ArrayList();
//private ArrayList rootEntries = new ArrayList();
//private ArrayList rootDirectories = new ArrayList();
private int count = 0;
private long totalSize = 0;
/**
* 缺省构造方法,一般用于创建一个新的压缩文件,但是是一个临时文件。
*/
public JZJFile() {
try {
selfFile = File.createTempFile("jzj", ".tmp", workDirectory);
}
catch (IOException e) {
}
}
/**
* 构造方法,如果指定的文件不存在则创建一个指定名称的新文件。
* @param fileName 文件名
*/
public JZJFile(String fileName) {
selfFile = new File(fileName);
createNewFile();
}
/**
* 构造方法,如果指定的文件不存在则创建一个指定名称的新文件。
* @param file 文件
*/
public JZJFile(File file) {
selfFile = file;
createNewFile();
}
/**
* 设置系统的工作目录,设置以后就不能修改,直到JVM退出。
* 如果指定的目录不存在则为缺省的当前目录
* @param directoryName 目录名
*/
public static void setWorkDirectory(String directoryName) {
if (haveSetWorkDirectory) {
return;
}
File tmp = new File(directoryName);
if (tmp.isDirectory()) {
workDirectory = tmp;
}
}
/**
* 设置系统的工作目录,设置以后就不能修改,直到JVM退出。
* 如果指定的目录不存在则为缺省的当前目录
* @param directory 目录
*/
public static void setWorkDirectory(File directory) {
if (haveSetWorkDirectory) {
return;
}
if (directory != null && directory.isDirectory()) {
workDirectory = directory;
}
}
/**
* 向自己代表的压缩包中新增文件,如果该文件已经存在则增加失败。
* @param path 新增的文件所在的目录
* @param fileName 新增的文件的文件名
*/
public void addFile(File path, String fileName) {
addFileToSelf(path, fileName, false);
}
/**
* 向自己代表的压缩包中新增文件。
* @param path 新增的文件所在的目录
* @param fileName 新增的文件的文件名
* @param overWrite 是否覆盖已有的文件
*/
public void addFile(File path, String fileName, boolean overWrite) {
addFileToSelf(path, fileName, overWrite);
}
/**
* 向自己代表的压缩包中新增文件。
* @param path 新增的文件所在的目录
* @param fileName 新增的文件的文件名
* @param overWrite 是否覆盖已有的文件
*/
private void addFileToSelf(File path, String fileName, boolean overWrite) {
BufferedInputStream bin = null;
ZipOutputStream zout = null;
ZipInputStream zin = null;
File tmpzip = null;
ZipEntry addedEntry = new ZipEntry(fileName);
try {
tmpzip = File.createTempFile("jzj", ".tmp", workDirectory);
zin = new ZipInputStream(new FileInputStream(selfFile));
zout = new ZipOutputStream(new FileOutputStream(tmpzip));
ZipEntry entry;
int len = 0;
byte[] b = new byte[4096];
while ( (entry = zin.getNextEntry()) != null) {
if (!isSameEntry(entry, addedEntry)) {
zout.putNextEntry(new ZipEntry(entry.getName()));
while ( (len = zin.read(b)) != -1) {
zout.write(b, 0, len);
}
zout.closeEntry();
zin.closeEntry();
}
else if (overWrite == true) {
continue;
}
else {
zout.close();
zin.close();
tmpzip.delete();
tmpzip = null;
throw new JZJException(JZJException.ENTRYEXIST);
}
}
bin = new BufferedInputStream(new FileInputStream(new File(path, fileName)));
zout.putNextEntry(addedEntry);
while ( (len = bin.read(b)) != -1) {
zout.write(b, 0, len);
}
zout.closeEntry();
zout.close();
zin.close();
String selfFileName = selfFile.getPath();
selfFile.delete();
tmpzip.renameTo(new File(selfFileName));
selfFile = new File(selfFileName);
isChanged = true;
count++;
}
catch (Exception e) {
if (tmpzip != null) {
tmpzip.delete();
}
e.printStackTrace();
}
}
/**
* 向自己代表的压缩包中新增多个文件,如果已经存在则不会添加。
* @param path 新增的文件所在的目录
* @param fileNames 新增的文件的文件名
*/
public void addFiles(File path, String[] fileNames) {
addFilesToSelf(path, fileNames, false);
}
/**
* 向自己代表的压缩包中新增多个文件。
* @param path 新增的文件所在的目录
* @param fileNames 新增的文件的文件名
* @param overWrite 设置在已经存在该文件时是否进行覆盖
*/
public void addFiles(File path, String[] fileNames, boolean overWrite) {
addFilesToSelf(path, fileNames, overWrite);
}
/**
* 向自己代表的压缩包中新增多个文件。
* @param path 新增的文件所在的目录
* @param fileNames 新增的文件的文件名
* @param overWrite 设置在已经存在该文件时是否进行覆盖
*/
private void addFilesToSelf(File path, String[] fileNames, boolean overWrite) {
BufferedInputStream bin = null;
ZipOutputStream zout = null;
ZipInputStream zin = null;
File tmpzip = null;
ZipEntry[] addedEntries = new ZipEntry[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
addedEntries[i] = new ZipEntry(fileNames[i]);
}
try {
tmpzip = File.createTempFile("jzj", ".tmp", workDirectory);
zin = new ZipInputStream(new FileInputStream(selfFile));
zout = new ZipOutputStream(new FileOutputStream(tmpzip));
ZipEntry entry;
int len = 0;
byte[] b = new byte[4096];
while ( (entry = zin.getNextEntry()) != null) {
if (!isContainEntry(entry, addedEntries)) {
zout.putNextEntry(new ZipEntry(entry.getName()));
while ( (len = zin.read(b)) != -1) {
zout.write(b, 0, len);
}
zout.closeEntry();
zin.closeEntry();
}
else if (overWrite == true) {
continue;
}
else {
zout.close();
zin.close();
tmpzip.delete();
tmpzip = null;
throw new JZJException(JZJException.ENTRYEXIST);
}
}
for (int i = 0; i < addedEntries.length; i++) {
bin = new BufferedInputStream(new FileInputStream(new File(path,
fileNames[i])));
zout.putNextEntry(addedEntries[i]);
while ( (len = bin.read(b)) != -1) {
zout.write(b, 0, len);
}
zout.closeEntry();
count++;
}
zout.close();
zin.close();
String slefFileName = selfFile.getPath();
logger.debug("rename to:" + slefFileName);
selfFile.delete();
tmpzip.renameTo(new File(slefFileName));
selfFile = new File(slefFileName);
isChanged = true;
}
catch (Exception e) {
if (tmpzip != null) {
tmpzip.delete();
}
e.printStackTrace();
}
}
/**
* 得到压缩包中的文件数。
* @return 压缩包中的文件数
*/
public int size() {
return count;
}
/**
* 得到压缩包中的所有文件的大小的和。
* @return 压缩包中的所有文件的大小的和
*/
public long getTotalSize() {
return totalSize;
}
/**
* 重新命名压缩包。
* @param fileName 期望的文件名
*/
public void renameTo(String fileName) {
if (fileName == null) {
return;
}
File target = new File(fileName);
if (target.exists()) {
return;
}
selfFile.renameTo(target);
}
/**
* 扫描压缩包取得全部需要的信息,如果已经扫描过而且自上次扫描后没有变化则不重新扫描。
*/
public void scan() {
if (selfFile == null) {
return;
}
if (isChanged || !isScaned) {
scanSelf();
}
}
/**
* 扫描压缩包取得全部需要的信息。
*/
public void rescan() {
if (selfFile == null) {
return;
}
scanSelf();
}
/**
* 判断自身是否包含指定的项目。
* @param entryName 项目名称
* @return 存在返回true,否则返回false
*/
public boolean isContainEntry(String entryName) {
scan();
return entryNames.contains(entryName);
}
/**
* 扫描压缩包取得全部需要的信息。
*/
private void scanSelf() {
entries.clear();
//rootEntries.clear();
//rootDirectories.clear();
count = 0;
JZJFileRecord record;
try {
ZipFile file = new ZipFile(selfFile);
Enumeration enum = file.entries();
ZipEntry entry;
while (enum.hasMoreElements()) {
entry = (ZipEntry) enum.nextElement();
if (entry.isDirectory() || entry.getName().equals("..\\")) {
continue;
}
record = new JZJFileRecord(entry);
entries.add(record);
totalSize = totalSize + record.getSize();
entryNames.add(record.entry.getName());
count++;
/*if (record.isRootEntry()) {
rootEntries.add(record);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -