if (ostream == null) {
if (outputFile == null) {
destfile = getConfigEntryResource().getFile();
if ((destfile == null) || !destfile.exists()) {
throw new ConfigException("Could not find " + getConfigEntryResource().getURL());
}
outputFile = new File(destfile.getParentFile(), destfile.getName() + ".tmp");
}
outputFile.getParentFile().mkdirs();
ostream = new BufferedOutputStream(new FileOutputStream(outputFile), 8192);
needCloseOutputStream = true;
}
// �����istream
if (istream == null) {
istream = getConfigEntryResource().getURL().openStream();
if (!(istream instanceof BufferedInputStream)) {
istream = new BufferedInputStream(istream, 8192);
}
needCloseInputStream = true;
}
zis = new ZipInputStream(istream);
zos = new ZipOutputStream(ostream);
ZipEntry zipEntry;
getGenerator().startSession(getConfigSettings().getPropertiesSet());
while ((zipEntry = zis.getNextEntry()) != null) {
allSuccess &= processZipEntry(zipEntry, zis, zos, dirs);
}
allSuccess &= getGenerator().getSession().generateLazyItems(new ZipCallback(zos, dirs));
getGenerator().getSession().checkNonprocessedTemplates();
getGenerator().getSession().generateLog(new ZipCallback(zos, dirs));
success = true;
} catch (IOException e) {
throw new ConfigException(e);
} finally {
getGenerator().closeSession();
// ����zip�ļ��������ر���
if (zos != null) {
try {
zos.finish();
} catch (IOException e) {
}
}
// �������������ɵ�ǰentry���Դģ��Źر���
if (needCloseInputStream && (istream != null)) {
try {
istream.close();
} catch (IOException e) {
}
}
// ������������ɵ�ǰentry���Դģ��Źر���
if (needCloseOutputStream && (ostream != null)) {
try {
ostream.flush();
ostream.close();
} catch (IOException e) {
}
// ����ɹ�������ʱ�ļ��ij���ʽ�ļ�������ɾ����ʱ�ļ�
if (success) {
// ����û��ָ��outputFileʱ�����������
if (getOutputFile() == null) {
// ��windows�£��۲쵽renameʧ�ܣ���Ϊlock��ԭ��һ�����ԡ�
int retryTimes = 10;
boolean succ = false;
String message = String.format("Moving file %s to %s failed.", outputFile.getName(),
destfile.getName());
for (int i = 0; i < retryTimes; i++) {
destfile.delete();
succ = outputFile.renameTo(destfile);
if (succ) {
break;
}
getConfigSettings().warn(
String.format(message + " Wait 0.5s and try again...%d of %d", i + 1, retryTimes));
try {
System.gc();
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
if (!succ) {
throw new ConfigException(message);
}
}
} else {
outputFile.delete();
}