File indexing completed on 2025-01-26 04:24:52

0001 #ifndef JLCOMPRESSFOLDER_H_
0002 #define JLCOMPRESSFOLDER_H_
0003 
0004 /*
0005 Copyright (C) 2010 Roberto Pompermaier
0006 Copyright (C) 2005-2016 Sergey A. Tachenov
0007 
0008 This file is part of QuaZIP.
0009 
0010 QuaZIP is free software: you can redistribute it and/or modify
0011 it under the terms of the GNU Lesser General Public License as published by
0012 the Free Software Foundation, either version 2.1 of the License, or
0013 (at your option) any later version.
0014 
0015 QuaZIP is distributed in the hope that it will be useful,
0016 but WITHOUT ANY WARRANTY; without even the implied warranty of
0017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018 GNU Lesser General Public License for more details.
0019 
0020 You should have received a copy of the GNU Lesser General Public License
0021 along with QuaZIP.  If not, see <http://www.gnu.org/licenses/>.
0022 
0023 See COPYING file for the full LGPL text.
0024 
0025 Original ZIP package is copyrighted by Gilles Vollant and contributors,
0026 see quazip/(un)zip.h files for details. Basically it's the zlib license.
0027 */
0028 
0029 #include "quazip.h"
0030 #include "quazipfile.h"
0031 #include "quazipfileinfo.h"
0032 #include <QString>
0033 #include <QDir>
0034 #include <QFileInfo>
0035 #include <QFile>
0036 
0037 /// Utility class for typical operations.
0038 /**
0039   This class contains a number of useful static functions to perform
0040   simple operations, such as mass ZIP packing or extraction.
0041   */
0042 class QUAZIP_EXPORT JlCompress {
0043 private:
0044     static QStringList extractDir(QuaZip &zip, const QString &dir);
0045     static QStringList getFileList(QuaZip *zip);
0046     static QString extractFile(QuaZip &zip, QString fileName, QString fileDest);
0047     static QStringList extractFiles(QuaZip &zip, const QStringList &files, const QString &dir);
0048     /// Compress a single file.
0049     /**
0050       \param zip Opened zip to compress the file to.
0051       \param fileName The full path to the source file.
0052       \param fileDest The full name of the file inside the archive.
0053       \return true if success, false otherwise.
0054       */
0055     static bool compressFile(QuaZip* zip, QString fileName, QString fileDest);
0056     /// Compress a subdirectory.
0057     /**
0058       \param parentZip Opened zip containing the parent directory.
0059       \param dir The full path to the directory to pack.
0060       \param parentDir The full path to the directory corresponding to
0061       the root of the ZIP.
0062       \param recursive Whether to pack sub-directories as well or only
0063       files.
0064       \return true if success, false otherwise.
0065       */
0066     static bool compressSubDir(QuaZip* parentZip, QString dir, QString parentDir, bool recursive,
0067                                QDir::Filters filters);
0068     /// Extract a single file.
0069     /**
0070       \param zip The opened zip archive to extract from.
0071       \param fileName The full name of the file to extract.
0072       \param fileDest The full path to the destination file.
0073       \return true if success, false otherwise.
0074       */
0075     static bool extractFile(QuaZip* zip, QString fileName, QString fileDest);
0076     /// Remove some files.
0077     /**
0078       \param listFile The list of files to remove.
0079       \return true if success, false otherwise.
0080       */
0081     static bool removeFile(QStringList listFile);
0082 
0083 public:
0084     /// Compress a single file.
0085     /**
0086       \param fileCompressed The name of the archive.
0087       \param file The file to compress.
0088       \return true if success, false otherwise.
0089       */
0090     static bool compressFile(QString fileCompressed, QString file);
0091     /// Compress a list of files.
0092     /**
0093       \param fileCompressed The name of the archive.
0094       \param files The file list to compress.
0095       \return true if success, false otherwise.
0096       */
0097     static bool compressFiles(QString fileCompressed, QStringList files);
0098     /// Compress a whole directory.
0099     /**
0100       Does not compress hidden files. See compressDir(QString, QString, bool, QDir::Filters).
0101 
0102       \param fileCompressed The name of the archive.
0103       \param dir The directory to compress.
0104       \param recursive Whether to pack the subdirectories as well, or
0105       just regular files.
0106       \return true if success, false otherwise.
0107       */
0108     static bool compressDir(QString fileCompressed, QString dir = QString(), bool recursive = true);
0109     /**
0110      * @brief Compress a whole directory.
0111      *
0112      * Unless filters are specified explicitly, packs
0113      * only regular non-hidden files (and subdirs, if @c recursive is true).
0114      * If filters are specified, they are OR-combined with
0115      * <tt>%QDir::AllDirs|%QDir::NoDotAndDotDot</tt> when searching for dirs
0116      * and with <tt>QDir::Files</tt> when searching for files.
0117      *
0118      * @param fileCompressed path to the resulting archive
0119      * @param dir path to the directory being compressed
0120      * @param recursive if true, then the subdirectories are packed as well
0121      * @param filters what to pack, filters are applied both when searching
0122      * for subdirs (if packing recursively) and when looking for files to pack
0123      * @return true on success, false otherwise
0124      */
0125     static bool compressDir(QString fileCompressed, QString dir,
0126                             bool recursive, QDir::Filters filters);
0127 
0128 public:
0129     /// Extract a single file.
0130     /**
0131       \param fileCompressed The name of the archive.
0132       \param fileName The file to extract.
0133       \param fileDest The destination file, assumed to be identical to
0134       \a file if left empty.
0135       \return The list of the full paths of the files extracted, empty on failure.
0136       */
0137     static QString extractFile(QString fileCompressed, QString fileName, QString fileDest = QString());
0138     /// Extract a list of files.
0139     /**
0140       \param fileCompressed The name of the archive.
0141       \param files The file list to extract.
0142       \param dir The directory to put the files to, the current
0143       directory if left empty.
0144       \return The list of the full paths of the files extracted, empty on failure.
0145       */
0146     static QStringList extractFiles(QString fileCompressed, QStringList files, QString dir = QString());
0147     /// Extract a whole archive.
0148     /**
0149       \param fileCompressed The name of the archive.
0150       \param dir The directory to extract to, the current directory if
0151       left empty.
0152       \return The list of the full paths of the files extracted, empty on failure.
0153       */
0154     static QStringList extractDir(QString fileCompressed, QString dir = QString());
0155     /// Get the file list.
0156     /**
0157       \return The list of the files in the archive, or, more precisely, the
0158       list of the entries, including both files and directories if they
0159       are present separately.
0160       */
0161     static QStringList getFileList(QString fileCompressed);
0162     /// Extract a single file.
0163     /**
0164       \param ioDevice pointer to device with compressed data.
0165       \param fileName The file to extract.
0166       \param fileDest The destination file, assumed to be identical to
0167       \a file if left empty.
0168       \return The list of the full paths of the files extracted, empty on failure.
0169       */
0170     static QString extractFile(QIODevice *ioDevice, QString fileName, QString fileDest = QString());
0171     /// Extract a list of files.
0172     /**
0173       \param ioDevice pointer to device with compressed data.
0174       \param files The file list to extract.
0175       \param dir The directory to put the files to, the current
0176       directory if left empty.
0177       \return The list of the full paths of the files extracted, empty on failure.
0178       */
0179     static QStringList extractFiles(QIODevice *ioDevice, QStringList files, QString dir = QString());
0180     /// Extract a whole archive.
0181     /**
0182       \param ioDevice pointer to device with compressed data.
0183       \param dir The directory to extract to, the current directory if
0184       left empty.
0185       \return The list of the full paths of the files extracted, empty on failure.
0186       */
0187     static QStringList extractDir(QIODevice *ioDevice, QString dir = QString());
0188     /// Get the file list.
0189     /**
0190       \return The list of the files in the archive, or, more precisely, the
0191       list of the entries, including both files and directories if they
0192       are present separately.
0193       */
0194     static QStringList getFileList(QIODevice *ioDevice); 
0195 };
0196 
0197 #endif /* JLCOMPRESSFOLDER_H_ */