📄 zipiomanager.java
字号:
/*
* Copyright (C) 1999-2005 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mandarax.zkb;
import java.util.Properties;
import java.util.zip.*;
import java.util.jar.*;
import java.io.*;
import java.net.URL;
import org.mandarax.zkb.IOManager.Data;
/**
* IO Manager implementation based on zip / jar files.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.4
*/
public class ZIPIOManager extends AbstractIOManager {
/**
* Write zkb data.
* @param target an object describing the target (e.g. a file)
* @param metaData the serialized meta data
* @param kbData the serialized knowledge base
* @param ops the object persistency service used
* @param resourceData the serialized resource data (objects)
*/
public void write(Object target,byte[] metaData, byte[] kbData, byte[] resourceData,ObjectPersistencyService ops) throws Exception {
OutputStream out = null;
if (target instanceof File) {
File f = (File)target;
if (f.isDirectory()) throw new IllegalArgumentException("target must not be a directory");
else out = new FileOutputStream(f);
}
else if (target instanceof OutputStream) out = (OutputStream)target;
else throw new IllegalArgumentException("This target type is not support: " + target);
JarOutputStream jarOut = new JarOutputStream(out);
JarEntry entry = new JarEntry(META);
jarOut.putNextEntry(entry);
jarOut.write(metaData);
jarOut.closeEntry();
entry = new JarEntry(RESOURCES + "." + ops.getExtension());
jarOut.putNextEntry(entry);
jarOut.write(resourceData);
jarOut.closeEntry();
entry = new JarEntry(KB);
jarOut.putNextEntry(entry);
jarOut.write(kbData);
jarOut.closeEntry();
jarOut.close();
out.close();
}
/**
* Read zkb data.
* @param source the data source (e.g., a file)
* @return a data object
*/
public Data read(Object source) throws Exception {
Data result = new Data();
File f = null;
InputStream in = null;
if (source instanceof File) {
f = (File)source;
if (f.isDirectory()) {
throw new IllegalArgumentException("target must not be a directory");
}
else in = new FileInputStream(f);
}
else if (source instanceof InputStream) in = (InputStream)source;
else if (source instanceof URL) in = ((URL)source).openStream();
else throw new IllegalArgumentException("This source type is not support: " + source);
JarInputStream jarIn = new JarInputStream(in);
// meta data
byte[] metaData = this.readFromZip(jarIn,META);
// read extension for resource file from meta data
result.metaData = new Properties();
result.metaData.load(new ByteArrayInputStream(metaData));
result.ops = getOPS(result.metaData);
result.resourceData = this.readFromZip(jarIn,RESOURCES+"."+result.ops.getExtension());
result.kbData = this.readFromZip(jarIn,KB);
jarIn.close();
in.close();
return result;
}
/**
* Read the next piece of data from a zip file.
* @param jarIn the jar input stream
* @param expectedName the expected name of the next zip entry
* @return an array of bytes
*/
private byte[] readFromZip(JarInputStream jarIn,String expectedName) throws ZKBException {
// kb
try {
JarEntry jarEntry = jarIn.getNextJarEntry();
if (!jarEntry.getName().equals(expectedName)) {
error("Zip entry " + expectedName + " expected but " + jarEntry.getName() + " found");
}
byte[] data = readData(jarIn);
jarIn.closeEntry();
return data;
}
catch (IOException x) {
error("Cannot read zkb kb from zip entry",x);
}
return new byte[0];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -