下面列出了java.util.zip.ZipEntry#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get an inputstream of a GPX file inside a zip
* @param inFile File object describing zip file
* @return input stream for Xml parser
*/
private static InputStream getZipInputStream(File inFile)
{
try
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
while (zis.available() > 0)
{
ZipEntry entry = zis.getNextEntry();
String entryName = entry.toString();
if (entryName != null && entryName.length() > 4)
{
String suffix = entryName.substring(entryName.length()-4).toLowerCase();
if (suffix.equals(".gpx") || suffix.equals(".xml")) {
// First matching file so must be gpx
return zis;
}
}
}
}
catch (Exception e) {} // ignore errors
// not found - error!
return null;
}
protected void getClasses(File archive, List<Class> l) {
try {
ZipInputStream zip = new ZipInputStream(new FileInputStream(archive));
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
String name = entry.toString();
if (name.startsWith("WEB-INF/classes/"))
name = name.substring("WEB-INF/classes/".length());
Class c = getClass(name);
if (c != null)
l.add(c);
}
zip.close();
} catch (IOException ignored) {
}
}
public void testTopElement() throws XdsInternalException, FactoryConfigurationError, IOException {
List<ZipEntry> content = getContentEntries(zip);
for (int i=0; i<content.size(); i++) {
ZipEntry entry = content.get(i);
String entryName = entry.toString();
InputStream is = zip.getInputStream(entry);
String entryString = Io.getStringFromInputStream(is);
System.out.println("entryString = " + entryString.substring(0, min(entryString.length(), 25)));
OMElement entryElement = Util.parse_xml(entryString);
assertTrue("name is " + entryElement.getLocalName(),
entryElement.getLocalName().equals("TestResults"));
}
}
static String normalizeEntryName(ZipEntry aEntry)
{
// Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
String entryName = aEntry.toString();
if (entryName.startsWith("/")) {
entryName = entryName.substring(1);
}
return entryName;
}
public static String normalizeEntryName(ZipEntry aEntry)
{
// Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
String entryName = aEntry.toString();
if (entryName.startsWith("/")) {
entryName = entryName.substring(1);
}
return entryName;
}
@Override
ClassFile getClassFile(final String name, final String suffix) throws IOException {
final ZipEntry entry = zipFile.getEntry(toEntryName(name, suffix));
if (entry == null) {
return null;
}
return new ClassFile() {
@Override
public String getBase() {
return zipFile.getName();
}
@Override
public InputStream getInputStream() throws IOException {
return zipFile.getInputStream(entry);
}
@Override
public String getPath() {
return entry.toString();
}
@Override
public long getSize() {
return entry.getSize();
}
@Override
public long getTime() {
return entry.getTime();
}
};
}
/**
* Open the selected file and select appropriate xml loader
* @param inFile File to open
*/
public void openFile(File inFile)
{
try
{
ZipFile file = new ZipFile(inFile);
Enumeration<?> entries = file.entries();
boolean xmlFound = false;
while (entries.hasMoreElements() && !xmlFound)
{
ZipEntry entry = (ZipEntry) entries.nextElement();
String entryName = entry.toString();
if (entryName != null && entryName.length() > 4)
{
String suffix = entryName.substring(entryName.length()-4).toLowerCase();
if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
{
_xmlLoader.reset();
// Parse the stream using either Xerces or java classes
_xmlLoader.parseXmlStream(file.getInputStream(entry));
XmlHandler handler = _xmlLoader.getHandler();
if (handler == null) {
_app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
}
else
{
// Send back to app
SourceInfo sourceInfo = new SourceInfo(inFile,
(handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
_app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
null, sourceInfo, handler.getTrackNameList(),
new MediaLinkInfo(inFile, handler.getLinkArray()));
xmlFound = true;
}
}
}
}
file.close();
// Check whether there was an xml file inside
if (!xmlFound) {
_app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
}
}
catch (Exception e) {
_app.showErrorMessageNoLookup("error.load.dialogtitle", e.getClass().getName() + "\n - " + e.getMessage());
}
}
/**
* Use the given stream to access a remote zip file
* @param inStream stream to use to access file
*/
public void openStream(InputStream inStream)
{
try
{
ZipInputStream zis = new ZipInputStream(inStream);
boolean xmlFound = false;
while (!xmlFound && zis.available() > 0)
{
ZipEntry entry = zis.getNextEntry();
String entryName = entry.toString();
if (entryName != null && entryName.length() > 4)
{
String suffix = entryName.substring(entryName.length()-4).toLowerCase();
if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
{
_xmlLoader.reset();
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.parse(zis, _xmlLoader);
XmlHandler handler = _xmlLoader.getHandler();
if (handler == null) {
_app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
}
else
{
// Send back to app
_app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES),
handler.getTrackNameList());
xmlFound = true;
}
}
}
}
// Check whether there was an xml file inside
if (!xmlFound) {
_app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
}
}
catch (Exception e) {
System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage());
}
}