2011-09-07 12:08:57 +02:00
|
|
|
//bzipParser.java
|
2005-05-17 10:25:04 +02:00
|
|
|
//------------------------
|
|
|
|
//part of YaCy
|
2008-07-20 19:14:51 +02:00
|
|
|
//(C) by Michael Peter Christen; mc@yacy.net
|
2005-05-17 10:25:04 +02:00
|
|
|
//first published on http://www.anomic.de
|
|
|
|
//Frankfurt, Germany, 2005
|
|
|
|
//
|
|
|
|
//this file is contributed by Martin Thelian
|
2009-07-09 22:13:11 +02:00
|
|
|
//
|
|
|
|
// $LastChangedDate$
|
|
|
|
// $LastChangedRevision$
|
|
|
|
// $LastChangedBy$
|
2005-05-17 10:25:04 +02:00
|
|
|
//
|
|
|
|
//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
|
|
|
|
|
2009-10-18 02:53:43 +02:00
|
|
|
package net.yacy.document.parser;
|
2005-05-17 10:25:04 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
2009-07-14 13:01:05 +02:00
|
|
|
|
2013-09-15 23:27:04 +02:00
|
|
|
import net.yacy.cora.document.id.AnchorURL;
|
2015-11-03 03:35:01 +01:00
|
|
|
import net.yacy.cora.document.id.DigestURL;
|
2009-10-18 02:53:43 +02:00
|
|
|
import net.yacy.document.AbstractParser;
|
|
|
|
import net.yacy.document.Document;
|
2010-06-29 21:20:45 +02:00
|
|
|
import net.yacy.document.Parser;
|
2009-10-20 00:34:44 +02:00
|
|
|
import net.yacy.document.TextParser;
|
2015-01-30 13:20:56 +01:00
|
|
|
import net.yacy.document.VocabularyScraper;
|
2009-10-10 03:14:19 +02:00
|
|
|
import net.yacy.kelondro.util.FileUtils;
|
|
|
|
|
2012-05-28 23:53:48 +02:00
|
|
|
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
|
2015-11-03 03:35:01 +01:00
|
|
|
import org.apache.commons.compress.compressors.bzip2.BZip2Utils;
|
2005-05-17 10:25:04 +02:00
|
|
|
|
2007-09-05 11:01:35 +02:00
|
|
|
|
2010-12-27 16:37:11 +01:00
|
|
|
public class bzipParser extends AbstractParser implements Parser {
|
2011-09-07 12:08:57 +02:00
|
|
|
|
|
|
|
public bzipParser() {
|
2010-06-29 21:20:45 +02:00
|
|
|
super("Bzip 2 UNIX Compressed File Parser");
|
2011-09-07 12:08:57 +02:00
|
|
|
this.SUPPORTED_EXTENSIONS.add("bz2");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("tbz");
|
|
|
|
this.SUPPORTED_EXTENSIONS.add("tbz2");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-bzip2");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/bzip2");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-bz2");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-bzip");
|
|
|
|
this.SUPPORTED_MIME_TYPES.add("application/x-stuffit");
|
2005-05-17 10:25:04 +02:00
|
|
|
}
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2012-04-24 16:07:03 +02:00
|
|
|
@Override
|
2015-04-15 13:17:23 +02:00
|
|
|
public Document[] parse(
|
|
|
|
final AnchorURL location,
|
|
|
|
final String mimeType,
|
|
|
|
final String charset,
|
|
|
|
final VocabularyScraper scraper,
|
|
|
|
final int timezoneOffset,
|
|
|
|
final InputStream source)
|
2010-12-27 16:37:11 +01:00
|
|
|
throws Parser.Failure, InterruptedException {
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
File tempFile = null;
|
2010-12-02 12:05:04 +01:00
|
|
|
Document[] docs;
|
2011-09-07 12:08:57 +02:00
|
|
|
try {
|
2005-05-17 10:25:04 +02:00
|
|
|
int read = 0;
|
2008-08-02 14:12:04 +02:00
|
|
|
final byte[] data = new byte[1024];
|
2015-11-03 03:35:01 +01:00
|
|
|
// BZip2CompressorInputStream checks filecontent (magic start-bytes "BZh") and throws ioexception if no match
|
2012-05-28 23:53:48 +02:00
|
|
|
final BZip2CompressorInputStream zippedContent = new BZip2CompressorInputStream(source);
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
tempFile = File.createTempFile("bunzip","tmp");
|
|
|
|
tempFile.deleteOnExit();
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
// creating a temp file to store the uncompressed data
|
2008-08-02 14:12:04 +02:00
|
|
|
final FileOutputStream out = new FileOutputStream(tempFile);
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
// reading gzip file and store it uncompressed
|
2006-09-03 16:59:00 +02:00
|
|
|
while((read = zippedContent.read(data, 0, 1024)) != -1) {
|
2005-05-17 10:25:04 +02:00
|
|
|
out.write(data, 0, read);
|
|
|
|
}
|
|
|
|
zippedContent.close();
|
|
|
|
out.close();
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2005-05-17 10:25:04 +02:00
|
|
|
// creating a new parser class to parse the unzipped content
|
2015-11-03 03:35:01 +01:00
|
|
|
final String contentfilename = BZip2Utils.getUncompressedFilename(location.getFileName());
|
|
|
|
final String mime = TextParser.mimeOf(DigestURL.getFileExtension(contentfilename));
|
|
|
|
docs = TextParser.parseSource(location, mime, null, scraper, timezoneOffset, 999, tempFile);
|
|
|
|
// TODO: this could return null from content parsing, even if bz2 successful read (see zipParser for alternative coding)
|
2011-09-07 12:08:57 +02:00
|
|
|
} catch (final Exception e) {
|
2006-09-03 16:59:00 +02:00
|
|
|
if (e instanceof InterruptedException) throw (InterruptedException) e;
|
2010-06-29 21:20:45 +02:00
|
|
|
if (e instanceof Parser.Failure) throw (Parser.Failure) e;
|
2011-09-07 12:08:57 +02:00
|
|
|
|
2010-06-29 21:20:45 +02:00
|
|
|
throw new Parser.Failure("Unexpected error while parsing bzip file. " + e.getMessage(),location);
|
2005-05-17 10:25:04 +02:00
|
|
|
} finally {
|
2009-03-30 17:31:25 +02:00
|
|
|
if (tempFile != null) FileUtils.deletedelete(tempFile);
|
2005-05-17 10:25:04 +02:00
|
|
|
}
|
2010-12-02 12:05:04 +01:00
|
|
|
return docs;
|
2005-05-17 10:25:04 +02:00
|
|
|
}
|
|
|
|
}
|