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.
133 lines
4.8 KiB
133 lines
4.8 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
|
//
|
||
|
// $LastChangedDate$
|
||
|
// $LastChangedRevision$
|
||
|
// $LastChangedBy$
|
||
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
|
||
|
|
||
15 years ago
|
package net.yacy.document.parser;
|
||
20 years ago
|
|
||
|
import java.io.File;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.InputStream;
|
||
16 years ago
|
import java.util.HashSet;
|
||
|
import java.util.Set;
|
||
|
|
||
15 years ago
|
import net.yacy.document.AbstractParser;
|
||
|
import net.yacy.document.Document;
|
||
|
import net.yacy.document.Idiom;
|
||
|
import net.yacy.document.Parser;
|
||
|
import net.yacy.document.ParserException;
|
||
15 years ago
|
import net.yacy.kelondro.data.meta.DigestURI;
|
||
15 years ago
|
import net.yacy.kelondro.util.FileUtils;
|
||
|
|
||
20 years ago
|
import org.apache.tools.bzip2.CBZip2InputStream;
|
||
|
|
||
17 years ago
|
|
||
16 years ago
|
public class bzipParser extends AbstractParser implements Idiom {
|
||
20 years ago
|
|
||
|
/**
|
||
|
* a list of mime types that are supported by this parser class
|
||
|
* @see #getSupportedMimeTypes()
|
||
|
*/
|
||
16 years ago
|
public static final Set<String> SUPPORTED_MIME_TYPES = new HashSet<String>();
|
||
|
public static final Set<String> SUPPORTED_EXTENSIONS = new HashSet<String>();
|
||
|
static {
|
||
|
SUPPORTED_EXTENSIONS.add("bz2");
|
||
|
SUPPORTED_EXTENSIONS.add("tbz");
|
||
|
SUPPORTED_EXTENSIONS.add("tbz2");
|
||
|
SUPPORTED_MIME_TYPES.add("application/x-bzip2");
|
||
|
SUPPORTED_MIME_TYPES.add("application/bzip2");
|
||
|
SUPPORTED_MIME_TYPES.add("application/x-bz2");
|
||
|
SUPPORTED_MIME_TYPES.add("application/x-bzip");
|
||
|
SUPPORTED_MIME_TYPES.add("application/x-stuffit");
|
||
16 years ago
|
}
|
||
19 years ago
|
|
||
20 years ago
|
public bzipParser() {
|
||
16 years ago
|
super("Bzip 2 UNIX Compressed File Parser");
|
||
20 years ago
|
}
|
||
|
|
||
16 years ago
|
public Set<String> supportedMimeTypes() {
|
||
20 years ago
|
return SUPPORTED_MIME_TYPES;
|
||
|
}
|
||
|
|
||
16 years ago
|
public Set<String> supportedExtensions() {
|
||
|
return SUPPORTED_EXTENSIONS;
|
||
|
}
|
||
|
|
||
15 years ago
|
public Document parse(final DigestURI 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
|
||
18 years ago
|
while((read = zippedContent.read(data, 0, 1024)) != -1) {
|
||
20 years ago
|
out.write(data, 0, read);
|
||
|
}
|
||
|
zippedContent.close();
|
||
|
out.close();
|
||
|
|
||
18 years ago
|
// check for interruption
|
||
|
checkInterruption();
|
||
|
|
||
20 years ago
|
// creating a new parser class to parse the unzipped content
|
||
16 years ago
|
return Parser.parseSource(location,null,null,tempFile);
|
||
17 years ago
|
} catch (final Exception e) {
|
||
18 years ago
|
if (e instanceof InterruptedException) throw (InterruptedException) e;
|
||
18 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
|
}
|
||
|
}
|
||
|
|
||
16 years ago
|
@Override
|
||
20 years ago
|
public void reset() {
|
||
18 years ago
|
// Nothing todo here at the moment
|
||
|
super.reset();
|
||
20 years ago
|
}
|
||
|
}
|