Subversion Repositories Scribus

Rev

Rev 23512 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18595 fschmid 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
/***************************************************************************
8
*                                                                         *
9
*   This program is free software; you can redistribute it and/or modify  *
10
*   it under the terms of the GNU General Public License as published by  *
11
*   the Free Software Foundation; either version 2 of the License, or     *
12
*   (at your option) any later version.                                   *
13
*                                                                         *
14
***************************************************************************/
15
/***************************************************************************
16
                                                         -------------------
17
        begin                : Sat Nov 23 2013
18
        copyright            : (C) 2013 by Franz Schmid
19
        email                : Franz.Schmid@altmuehlnet.de
20
 ***************************************************************************/
21
 
22
#include "zip.h"
23
#include "unzip.h"
24
#include "scribus_zip.h"
25
#include "util.h"
18913 fschmid 26
#include "scpaths.h"
18595 fschmid 27
 
28
ScZipHandler::ScZipHandler(bool forWrite)
29
{
30
        if (forWrite)
31
                m_zi = new Zip();
32
        else
33
                m_uz = new UnZip();
34
}
35
 
36
ScZipHandler::~ScZipHandler()
37
{
23512 jghali 38
        if (m_uz && m_uz->isOpen())
39
                m_uz->closeArchive();
40
        if (m_zi && m_zi->isOpen())
41
                m_zi->closeArchive();
22635 craig 42
        delete m_uz;
43
        delete m_zi;
18595 fschmid 44
}
45
 
22635 craig 46
bool ScZipHandler::open(const QString& fileName)
18595 fschmid 47
{
48
        bool retVal = false;
22635 craig 49
        if (m_uz != nullptr)
18595 fschmid 50
        {
51
                UnZip::ErrorCode ec = m_uz->openArchive(fileName);
52
                retVal = (ec == UnZip::Ok);
53
        }
22635 craig 54
        if (m_zi != nullptr)
18595 fschmid 55
        {
56
                Zip::ErrorCode ec = m_zi->createArchive(fileName);
57
                retVal = (ec == Zip::Ok);
58
        }
59
        return retVal;
60
}
61
 
62
bool ScZipHandler::close()
63
{
64
        bool retVal = false;
22635 craig 65
        if (m_uz != nullptr)
18595 fschmid 66
        {
67
                m_uz->closeArchive();
68
                retVal = true;
69
        }
22635 craig 70
        if (m_zi != nullptr)
18595 fschmid 71
        {
72
                Zip::ErrorCode ec = m_zi->closeArchive();
73
                retVal = (ec == Zip::Ok);
74
        }
75
        return retVal;
76
}
77
 
22635 craig 78
bool ScZipHandler::contains(const QString& fileName)
18595 fschmid 79
{
22635 craig 80
        if (m_uz == nullptr)
81
                return false;
82
        return m_uz->contains(fileName);
18595 fschmid 83
}
84
 
22635 craig 85
bool ScZipHandler::read(const QString& fileName, QByteArray &buf)
18595 fschmid 86
{
22635 craig 87
        if (m_uz == nullptr)
88
                return false;
89
        QByteArray byteArray;
90
        QBuffer buffer(&byteArray);
91
        buffer.open(QIODevice::WriteOnly);
92
        UnZip::ErrorCode ec = m_uz->extractFile(fileName, &buffer);
93
        bool retVal = (ec == UnZip::Ok);
94
        if (retVal)
95
                buf = byteArray;
18595 fschmid 96
        return retVal;
97
}
98
 
22635 craig 99
bool ScZipHandler::write(const QString& dirName)
18604 fschmid 100
{
22636 jghali 101
        if (m_zi == nullptr)
22635 craig 102
                return false;
103
        Zip::ErrorCode ec = m_zi->addDirectory(dirName, "", Zip::IgnoreRoot);
104
        return (ec == Zip::Ok);
18604 fschmid 105
}
106
 
22635 craig 107
bool ScZipHandler::extract(const QString& name, const QString& path, ExtractionOption eo)
18913 fschmid 108
{
22635 craig 109
        if (m_uz == nullptr)
110
                return false;
18913 fschmid 111
        bool retVal = false;
22635 craig 112
        QString pwd(QDir::currentPath());
113
        QString outDir;
114
        if (path.isNull())
115
                outDir = ScPaths::tempFileDir();
116
        else
117
                outDir=path;
118
        QFile f(outDir);
119
        QFileInfo fi(f);
120
        if (!fi.isWritable())
121
                outDir = ScPaths::applicationDataDir();
122
        QDir::setCurrent(outDir);
123
        UnZip::ErrorCode ec = m_uz->extractFile(name, outDir, static_cast<UnZip::ExtractionOption>(eo));
124
        retVal = (ec == UnZip::Ok);
125
        QDir::setCurrent(pwd);
18913 fschmid 126
        return retVal;
127
}
128
 
18595 fschmid 129
QStringList ScZipHandler::files()
130
{
22635 craig 131
        QStringList retVal;
132
        if (m_uz != nullptr)
18595 fschmid 133
                retVal = m_uz->fileList();
134
        return retVal;
135
}