You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.7 KiB
125 lines
4.7 KiB
20 years ago
|
//bzipParser.java
|
||
|
//------------------------
|
||
|
//part of YaCy
|
||
17 years ago
|
//(C) by Michael Peter Christen; mc@yacy.net
|
||
20 years ago
|
//first published on http://www.anomic.de
|
||
|
//Frankfurt, Germany, 2005
|
||
|
//
|
||
|
//this file is contributed by Martin Thelian
|
||
16 years ago
|
//last major change: 16.05.2005
|
||
20 years ago
|
//
|
||
|
//This program is free software; you can redistribute it and/or modify
|
||
|
//it under the terms of the GNU General Public License as published by
|
||
|
//the Free Software Foundation; either version 2 of the License, or
|
||
|
//(at your option) any later version.
|
||
|
//
|
||
|
//This program 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 General Public License for more details.
|
||
|
//
|
||
|
//You should have received a copy of the GNU General Public License
|
||
|
//along with this program; if not, write to the Free Software
|
||
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
16 years ago
|
package de.anomic.document.parser;
|
||
20 years ago
|
|
||
|
import java.io.File;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.InputStream;
|
||
|
import java.util.Hashtable;
|
||
|
|
||
|
import org.apache.tools.bzip2.CBZip2InputStream;
|
||
|
|
||
16 years ago
|
import de.anomic.document.AbstractParser;
|
||
|
import de.anomic.document.Parser;
|
||
|
import de.anomic.document.ParserDispatcher;
|
||
|
import de.anomic.document.ParserException;
|
||
|
import de.anomic.document.Document;
|
||
16 years ago
|
import de.anomic.kelondro.util.FileUtils;
|
||
18 years ago
|
import de.anomic.yacy.yacyURL;
|
||
|
|
||
20 years ago
|
public class bzipParser extends AbstractParser implements Parser {
|
||
|
|
||
|
/**
|
||
|
* a list of mime types that are supported by this parser class
|
||
|
* @see #getSupportedMimeTypes()
|
||
|
*/
|
||
17 years ago
|
public static final Hashtable<String, String> SUPPORTED_MIME_TYPES = new Hashtable<String, String>();
|
||
16 years ago
|
static String fileExtensions = "bz2,tbz,tbz2";
|
||
20 years ago
|
static {
|
||
19 years ago
|
SUPPORTED_MIME_TYPES.put("application/x-bzip2",fileExtensions);
|
||
|
SUPPORTED_MIME_TYPES.put("application/bzip2", fileExtensions);
|
||
|
SUPPORTED_MIME_TYPES.put("application/x-bz2", fileExtensions);
|
||
20 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* a list of library names that are needed by this parser
|
||
|
* @see Parser#getLibxDependences()
|
||
|
*/
|
||
16 years ago
|
private static final String[] LIBX_DEPENDENCIES = new String[] {};
|
||
19 years ago
|
|
||
20 years ago
|
public bzipParser() {
|
||
|
super(LIBX_DEPENDENCIES);
|
||
19 years ago
|
this.parserName = "Bzip 2 UNIX Compressed File Parser";
|
||
20 years ago
|
}
|
||
|
|
||
17 years ago
|
public Hashtable<String, String> getSupportedMimeTypes() {
|
||
20 years ago
|
return SUPPORTED_MIME_TYPES;
|
||
|
}
|
||
|
|
||
16 years ago
|
public Document parse(final yacyURL location, final String mimeType, final String charset, final InputStream source) throws ParserException, InterruptedException {
|
||
20 years ago
|
|
||
|
File tempFile = null;
|
||
|
try {
|
||
|
/*
|
||
|
* First we have to consume the first two char from the stream. Otherwise
|
||
|
* the bzip decompression will fail with a nullpointerException!
|
||
|
*/
|
||
|
int b = source.read();
|
||
|
if (b != 'B') {
|
||
|
throw new Exception("Invalid bz2 content.");
|
||
|
}
|
||
|
b = source.read();
|
||
|
if (b != 'Z') {
|
||
|
throw new Exception("Invalid bz2 content.");
|
||
|
}
|
||
|
|
||
|
int read = 0;
|
||
17 years ago
|
final byte[] data = new byte[1024];
|
||
|
final CBZip2InputStream zippedContent = new CBZip2InputStream(source);
|
||
20 years ago
|
|
||
|
tempFile = File.createTempFile("bunzip","tmp");
|
||
|
tempFile.deleteOnExit();
|
||
|
|
||
|
// creating a temp file to store the uncompressed data
|
||
17 years ago
|
final FileOutputStream out = new FileOutputStream(tempFile);
|
||
20 years ago
|
|
||
|
// reading gzip file and store it uncompressed
|
||
19 years ago
|
while((read = zippedContent.read(data, 0, 1024)) != -1) {
|
||
20 years ago
|
out.write(data, 0, read);
|
||
|
}
|
||
|
zippedContent.close();
|
||
|
out.close();
|
||
|
|
||
19 years ago
|
// check for interruption
|
||
|
checkInterruption();
|
||
|
|
||
20 years ago
|
// creating a new parser class to parse the unzipped content
|
||
16 years ago
|
return ParserDispatcher.parseSource(location,null,null,tempFile);
|
||
17 years ago
|
} catch (final Exception e) {
|
||
19 years ago
|
if (e instanceof InterruptedException) throw (InterruptedException) e;
|
||
19 years ago
|
if (e instanceof ParserException) throw (ParserException) e;
|
||
|
|
||
|
throw new ParserException("Unexpected error while parsing bzip file. " + e.getMessage(),location);
|
||
20 years ago
|
} finally {
|
||
16 years ago
|
if (tempFile != null) FileUtils.deletedelete(tempFile);
|
||
20 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public void reset() {
|
||
19 years ago
|
// Nothing todo here at the moment
|
||
|
super.reset();
|
||
20 years ago
|
}
|
||
|
}
|