QGpCoreTools/Tar.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2006-11-10
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef TAR_H
00029 #define TAR_H
00030 
00031 #include <zlib.h>
00032 
00033 #include "QGpCoreToolsDLLExport.h"
00034 #include "Translations.h"
00035 #include "CoreApplication.h"
00036 #include "Trace.h"
00037 
00038 namespace QGpCoreTools {
00039 
00040 class TarHeader;
00041 
00042 class QGPCORETOOLS_EXPORT Tar
00043 {
00044   TRANSLATIONS("Tar")
00045 public:
00046   Tar();
00047   ~Tar();
00048 
00049   bool open(QString file, QIODevice::OpenMode m);
00050   void close();
00051   QString fileName() const {return _fileName;}
00052 
00053   bool addFile(QString fileName, const QByteArray& data);
00054   bool nextFile(QString& fileName, QByteArray& data, bool cache=false  );
00055   bool file(const QString fileName, QByteArray& data);
00056   bool rewind();
00057 private:
00058   inline bool writeBlock(const char * buffer);
00059   inline bool readBlock(char * buffer);
00060   char * int2octal(int val);
00061   int octal2int(const char * octStr);
00062   uint checksum(TarHeader& fh);
00063   bool readFile(TarHeader& fh, QByteArray& data);
00064 
00065   gzFile _f;
00066   QString _fileName;
00067   QIODevice::OpenMode _mode;
00068   QCache<QString, QByteArray> _cachedFiles;
00069   QMap<QString, z_off_t> _fileOffsets;
00070 };
00071 
00072 inline bool Tar::writeBlock(const char * buffer)
00073 {
00074   TRACE;
00075   // To ensure compatibility with old zlib releases (void *) is requested.
00076   if(gzwrite (_f, (void *) buffer, 512)!=512) {
00077     App::stream() << tr("Cannot write block in tar file") << endl;
00078     return false;
00079   } else {
00080     return true;
00081   }
00082 }
00083 
00084 inline bool Tar::readBlock(char * buffer)
00085 {
00086   TRACE;
00087   int nb=gzread (_f, buffer, 512);
00088   if(nb!=512) {
00089     App::stream() << tr("Cannot read a block of 512 bytes in tar file") << endl;
00090     return false;
00091   } else {
00092     return true;
00093   }
00094 }
00095 
00096 } // namespace QGpCoreTools
00097 
00098 #endif // TAR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines