1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/***************************************************************************
extract.cpp - description
-------------------
begin : Sat Dec 1 2001
copyright : (C) 2001 by Richard Groult, 2003 OGINO Tomonori, 2003 Ian Monroe
email : rgroult@jalix.org ogino@nn.iij4u.or.jp ian@monroe.nu
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
* For license exceptions see LICENSE.EXC file, attached with the source *
* code. *
* *
***************************************************************************/
#define MYDEBUG kdDebug()<<__FILE__<<" " <<__LINE__ << " " << __FUNCTION__ << " "
#include "extract.h"
// KDE
#include <kprocess.h>
#include <kio/job.h>
#include <kzip.h>
#include <kar.h>
#include <ktar.h>
#include <kurl.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <kapplication.h>
#include <kstandarddirs.h>
// Qt
#include <qdir.h>
#include <qvaluelist.h>
void
Extract::getEntryRecursive( const KArchiveDirectory * entry, const QString& path )
{
QStringList const & entries(entry->entries());
QStringList::const_iterator it = entries.begin();
for(; it != entries.end(); ++it) {
QString add = path + QChar('/') + *it;
files += add;
const KArchiveEntry * child = entry->entry(*it);
if( child->isDirectory() ){
const KArchiveDirectory * childdir;
childdir = dynamic_cast<const KArchiveDirectory *>(child);
getEntryRecursive( childdir, path + QChar('/') + *it );
}
}
}
Extract::Extract (const QString& _filename)
{
QFileInfo fileinfo ( _filename );
QString tmpdir = locateLocal("tmp", "showimg-cpr/");
dest = tmpdir + '/' + fileinfo.fileName() + '/';
ext = fileinfo.extension(false).lower();
// Warn when the size of archive more than 'big(64MB).'
QFile::Offset big = 0x4000000;// 64MB
QFile qfile( _filename );
if( qfile.size() > big ) {
QString msg = QString( i18n("The size of selected archive seems to be too big;\ncontinue? (size: %1MB)") ).arg( (qfile.size())>>20 );
switch( KMessageBox::warningContinueCancel( 0, msg, i18n("Confirm")) ) {
case KMessageBox::Cancel :
return;
break;
}
}
KMimeType::Ptr mime = KMimeType::findByPath( _filename );
// KArchive - sync
KArchive * arc=NULL;
if ( mime->name() == "application/x-zip" )
arc = new KZip( fileinfo.absFilePath() );
else if ( mime->name() == "application/x-tar"
|| mime->name() == "application/x-tarz"
|| mime->name() == "application/x-tgz"
|| mime->name() == "application/x-tbz" )
arc = new KTar( fileinfo.absFilePath() );
else if ( mime->name() == "application/x-archive" )
arc = new KAr( fileinfo.absFilePath() );
if(arc)
{
if ( arc->open( IO_ReadOnly ) ) {
const KArchiveDirectory * dir = arc->directory();
dir->copyTo( dest );
getEntryRecursive( dir, QString() );
}
files.sort();
}
else
KMessageBox::error( 0,
"<qt>"+i18n("Unable to open the archive '<b>%1</b>'.").arg(fileinfo.absFilePath())+"</qt>",
i18n("Archive Error"));
}
bool
Extract::canExtract( const QString& _filename )
{
QFileInfo info( _filename );
//Use KMime to check filetypes out.
KMimeType::Ptr mime;
mime = KMimeType::findByPath( _filename, 0, true );
#if KDE_VERSION < 0x30200
if( mime->name() == KMimeType::defaultMimeType())
mime = KMimeType::findByFileContent( _filename );
if( mime->name() == "application/x-zip"
|| mime->name() == "application/x-tar"
|| mime->name() == "application/x-tarz"
|| mime->name() == "application/x-tgz"
|| mime->name() == "application/x-tbz"
|| mime->name() == "application/x-archive" )
return true;
#else
if( mime->is(KMimeType::defaultMimeType()))
mime = KMimeType::findByFileContent( _filename );
if( mime->is("application/x-zip")
|| mime->is("application/x-tar")
|| mime->is("application/x-tarz")
|| mime->is("application/x-tgz")
|| mime->is("application/x-tbz")
|| mime->is("application/x-archive") )
return true;
#endif
return false;
}
|