Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, July 2006
Creating a zip file from a directory

Pages: Prev | 1 | 2 | Next

Message 1 in thread

Hi all,

is there some easy way of compressing the content of a directory 
(including sub-dirs) to a zip file and vv with Qt4? I know of the Qt 
Solution named QtIOCompressor, but have no idea how to use that for 
(de)compressing a set of files.... actually, compression is not really 
that important for my application. If you know a way to merge a 
directory into one file and re-expand it, I'd be more than content as 
well :)

Regards,
M

--
 [ signature omitted ] 

Message 2 in thread

Martin wrote:
>
> Hi all,
>
> is there some easy way of compressing the content of a directory
> (including sub-dirs) to a zip file and vv with Qt4? I know of the Qt
> Solution named QtIOCompressor, but have no idea how to use that for
> (de)compressing a set of files.... actually, compression is not really
> that important for my application. If you know a way to merge a
> directory into one file and re-expand it, I'd be more than content as
> well :)
>
Another option might be to use zlib.
http://www.zlib.net/


Best regards,


Andre

--
 [ signature omitted ] 

Message 3 in thread

> > is there some easy way of compressing the content of a directory
> > (including sub-dirs) to a zip file and vv with Qt4? I know of the Qt

Hmm - do you really need to integrate everything within your program? 
Maybe NSIS Install System would work for you as well ... I don't know what 
exactly you want to do ...

Regards,
Malte

--
 [ signature omitted ] 

Message 4 in thread

Martin wrote:

> is there some easy way of compressing the content of a directory
> (including sub-dirs) to a zip file

Perhaps a QProcess using "zip" and "unzip" would work? It depends on
what exactly you want to accomplish.

> and vv with Qt4?

What does that mean?

Regards,


Björn

-- 
 [ signature omitted ] 

Message 5 in thread

> > and vv with Qt4?
> 
> What does that mean?

"vice-versa" i guess ...

Malte

--
 [ signature omitted ] 

Message 6 in thread

On Monday 03 July 2006 10:29 pm, Martin wrote:
> Hi all,
>
> is there some easy way of compressing the content of a directory
> (including sub-dirs) to a zip file and vv with Qt4?

Take a look at physfs 

http://icculus.org/physfs/


Julian.

-- 
 [ signature omitted ] 

Message 7 in thread

Martin wrote:
> Hi all,
> 
> is there some easy way of compressing the content of a directory 
> (including sub-dirs) to a zip file and vv with Qt4? I know of the Qt 
> Solution named QtIOCompressor, but have no idea how to use that for 
> (de)compressing a set of files.... actually, compression is not really 
> that important for my application. If you know a way to merge a 
> directory into one file and re-expand it, I'd be more than content as 
> well :)
> 
> Regards,
> M

Thanks everyone. Again, one more sentence would have explained a lot... 
I want to do this for a project import/export mechanism. In my 
application, a 'project' consists of a few files. Users need to pass 
their projects to other users, e.g. via e-mail. Doing this with a single 
zip file would be much more convenient than having to attach a whole 
directory... but anyway, if there is no at-most-three-hours-Qt-only way 
of doing it, this feature is cancelled.

Best,
M

P.S.: with vv, I indeed meant vice-versa :)

--
 [ signature omitted ] 

Message 8 in thread

On Mon, Jul 03, Martin wrote:

> Hi all,
> 
> is there some easy way of compressing the content of a directory 
> (including sub-dirs) to a zip file and vv with Qt4? I know of the Qt 
> Solution named QtIOCompressor, but have no idea how to use that for 
> (de)compressing a set of files.... actually, compression is not really 
> that important for my application. If you know a way to merge a 
> directory into one file and re-expand it, I'd be more than content as 
> well :)
> 

I use the following, though this still could be improved...

ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
{
    ErrorCode err=success;
    
    // zip the temporary directory
    Process *zipProc=new Process ();
    zipProc->clearArguments();
    zipProc->setWorkingDirectory (QDir(zipDir));
    zipProc->addArgument ("zip");
    zipProc->addArgument ("-r");
    zipProc->addArgument (zipName);
    zipProc->addArgument (".");

    if (!zipProc->start() )
    {   
        // zip could not be started
        QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
                       QObject::tr("Couldn't start zip to compress data."));
        err=aborted;
    } else
    {
        // zip could be started
        zipProc->waitFinished();
        if (!zipProc->normalExit() )
        {
            QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
                           QObject::tr("zip didn't exit normally")+
                           "\n" + zipProc->getErrout());
            err=aborted;
        } else
        {
            if (zipProc->exitStatus()>0)
            {
                QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
                           QString("zip exit code:  %1").arg(zipProc->exitStatus() )+
                           "\n" + zipProc->getErrout() );
                err=aborted;
            }
        }
    }   // zip could be started
    return err; 
}


Process is a slightly overloaded QProcess.


hth

-Uwe

--
 [ signature omitted ] 

Message 9 in thread

Uwe Drechsel wrote:
> On Mon, Jul 03, Martin wrote:
> 
>> Hi all,
>>
>> is there some easy way of compressing the content of a directory 
>> (including sub-dirs) to a zip file and vv with Qt4? I know of the Qt 
>> Solution named QtIOCompressor, but have no idea how to use that for 
>> (de)compressing a set of files.... actually, compression is not really 
>> that important for my application. If you know a way to merge a 
>> directory into one file and re-expand it, I'd be more than content as 
>> well :)
>>
> 
> I use the following, though this still could be improved...
> 
> ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
> {
>     ErrorCode err=success;
>     
>     // zip the temporary directory
>     Process *zipProc=new Process ();
>     zipProc->clearArguments();
>     zipProc->setWorkingDirectory (QDir(zipDir));
>     zipProc->addArgument ("zip");
>     zipProc->addArgument ("-r");
>     zipProc->addArgument (zipName);
>     zipProc->addArgument (".");
> 
>     if (!zipProc->start() )
>     {   
>         // zip could not be started
>         QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
>                        QObject::tr("Couldn't start zip to compress data."));
>         err=aborted;
>     } else
>     {
>         // zip could be started
>         zipProc->waitFinished();
>         if (!zipProc->normalExit() )
>         {
>             QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
>                            QObject::tr("zip didn't exit normally")+
>                            "\n" + zipProc->getErrout());
>             err=aborted;
>         } else
>         {
>             if (zipProc->exitStatus()>0)
>             {
>                 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
>                            QString("zip exit code:  %1").arg(zipProc->exitStatus() )+
>                            "\n" + zipProc->getErrout() );
>                 err=aborted;
>             }
>         }
>     }   // zip could be started
>     return err; 
> }
> 
> 
> Process is a slightly overloaded QProcess.


That would do exactly what I want, only that my application 
(unfortunately) is Windows-only at the moment, and I can't assume that 
zip is installed on the end-user machines :(

But thanks anyway!

M

--
 [ signature omitted ] 

Message 10 in thread

"Martin" <martin.umgeher@xxxxxxxxxxx> wrote in message 
news:e8b658$i7e$1@xxxxxxxxxxxxxxxxxxxxx

> That would do exactly what I want, only that my application 
> (unfortunately) is Windows-only at the moment, and I can't assume that zip 
> is installed on the end-user machines :(
>
> But thanks anyway!

If you have a Qt license, why not use zlib? 


--
 [ signature omitted ] 

Message 11 in thread

>> That would do exactly what I want, only that my application 
>> (unfortunately) is Windows-only at the moment, and I can't assume that zip 
>> is installed on the end-user machines :(
>>
>> But thanks anyway!
> If you have a Qt license, why not use zlib? 

Yes, you can easily do an implementation which simply uses the Qt 
functions:
     QByteArray qCompress(const QByteArray &data, int level)
     QByteArray qUncompress(const QByteArray &data)

which you can find in the docs here:
     http://doc.trolltech.com/4.1/qbytearray.html#qCompress

Those functions use the zlib library, which could be either a system 
library, or the Qt provided zlib.

-- 
 [ signature omitted ] 

Message 12 in thread

Marius Storm-Olsen wrote:
>>> That would do exactly what I want, only that my application 
>>> (unfortunately) is Windows-only at the moment, and I can't assume 
>>> that zip is installed on the end-user machines :(
>>>
>>> But thanks anyway!
>> If you have a Qt license, why not use zlib? 
> 
> Yes, you can easily do an implementation which simply uses the Qt 
> functions:
>     QByteArray qCompress(const QByteArray &data, int level)
>     QByteArray qUncompress(const QByteArray &data)
> 
> which you can find in the docs here:
>     http://doc.trolltech.com/4.1/qbytearray.html#qCompress
> 
> Those functions use the zlib library, which could be either a system 
> library, or the Qt provided zlib.
> 

Sorry, I still don't get it. How can I use QByteArray-compressing 
functions for compressing files and directories? I don't want to 
hard-code the file names and the directory structure, so the program 
doesn't know what files are there; it just should zip everything that's 
in a certain directory.

M

--
 [ signature omitted ] 

Message 13 in thread

> Sorry, I still don't get it. How can I use QByteArray-compressing 
> functions for compressing files and directories? I don't want to hard-code 
> the file names and the directory structure, so the program doesn't know 
> what files are there; it just should zip everything that's in a certain 
> directory.


With zlib you can add files to a zip.  With qt you can get
all of the filenames (look at the source for QFileDialog for example)
 or you can use boost::filesystem.
Not sure how to do this with QByteArray directly, at least not to preserve
directory structures.

Or try support@xxxxxxxxxxxxxx 


--
 [ signature omitted ] 

Message 14 in thread

Duane Hebert schrieb:
>> ...
> With zlib you can add files to a zip.  With qt you can get
> all of the filenames (look at the source for QFileDialog for example)
>  or you can use boost::filesystem.

Sorry, haven't followed this thread from the very beginning, but just to 
make something clear: Qt's zip functionality (as indicated in previous 
postings) does _not_ create a ZIP file, it merely uses the 
zip-compression scheme as to compress a given data QByteStream and 
outputs a compressed QByteStream. Period.

That said, if you really want to compress the files in a given directory 
you have to implement your own directory iterator (QDir would come handy 
here) which opens each file (QFile...), reads it into a QByteArray, 
compress it and append it to the existing, already compressed data stream.

But that still wouldn't solve the issue of decompression and re-creating 
each single QFile again out of the combined QByteArray! You still would 
  have to insert data separators (by encoding the comressed file length 
and using QByteArray qUncompress ( const uchar * data, int nbytes )), 
add the original (relative) file paths into this data stream etc. ... 
and finally you would probably end up in implementing something like 
WinZIP :)

At least that's how it would work with Qt3, not sure if Qt4 offers more 
"WinZIP-style" functionality.


Cheers, Oliver

--
 [ signature omitted ] 

Message 15 in thread

Till Oliver Knoll schrieb:
> Duane Hebert schrieb:
>>> ...
> ...
> postings) does _not_ create a ZIP file, it merely uses the 
> zip-compression scheme as to compress a given data QByteStream and 
> outputs a compressed QByteStream. Period.

Errr... sorry, that should read QByteArray, not QByteStream ;)

Cheers, Oliver


--
 [ signature omitted ] 

Pages: Prev | 1 | 2 | Next