[Igarden-commit] CommonSource/YAAFExtensions RGZipFile.cpp, NONE, 1.1.2.1 RGZipFile.h, NONE, 1.1.2.
Status: Beta
Brought to you by:
jlbedell
|
From: Jeffrey B. <jlb...@us...> - 2006-12-02 05:19:25
|
Update of /cvsroot/igarden/CommonSource/YAAFExtensions In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv19474 Added Files: Tag: Release_branch_v1 RGZipFile.cpp RGZipFile.h Log Message: Initial Checkin --- NEW FILE: RGZipFile.cpp --- /********************************************************************** RGZipFile class Copyright 2004 by Jeffrey Bedell Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Neither the name of the copyright holder or other contributors may be used to promote products derived from this software without specific written prior permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Contact: je...@re... **********************************************************************/ #include "RGZipFile.h" #include "RGDateTime.h" #include "RGDateTime.h" // --------------------------------------------------------------------------- /// RGZipFile // RGZipFile::RGZipFile() { } // --------------------------------------------------------------------------- /// ~RGZipFile // RGZipFile::~RGZipFile() { } // --------------------------------------------------------------------------- /// Open // ErrorCode RGZipFile::Open(XGFileSpecifier *fileSpec, bool rwflag) { UInt8 pathname[256]; #if OPT_MACOS FSSpec fsspec; FSRef fsref; OSErr err; _unzf = NULL; _zf = NULL; fsspec = fileSpec->GetFileName(); err = FSpMakeFSRef(&fsspec, &fsref); if(err == 0) { err = FSRefMakePath(&fsref, pathname, sizeof(pathname)); if(err == 0) _unzf = unzOpen(reinterpret_cast<const char*>(pathname)); } #else #error TO_BE_PORTED #endif _writable = rwflag; if(_unzf == NULL) return KYAAFFileOpenError; else return KYAAFNoError; } // --------------------------------------------------------------------------- /// Create // #if OPT_MACOS ErrorCode RGZipFile::Create(XGFileSpecifier *fileSpec, OSType osType, OSType creator) { UInt8 pathname[256]; FSSpec fsspec; FSRef fsref; OSErr err; _unzf = NULL; _zf = NULL; fsspec = fileSpec->GetFileName(); if (GOSFeatures.fHasFSSpec) { err = ::FSpCreate(&fsspec,creator,osType,smSystemScript); } else { err = ::HCreate(fsspec.vRefNum, fsspec.parID, fsspec.name, creator, osType); } err = FSpMakeFSRef(&fsspec, &fsref); if(err == 0) { err = FSRefMakePath(&fsref, pathname, sizeof(pathname)); if(err == 0) _zf = zipOpen(reinterpret_cast<const char*>(pathname), APPEND_STATUS_CREATE); } _writable = true; if(err != 0 || _zf == NULL) return KYAAFFileCreateError; else return KYAAFNoError; } #endif #if OPT_WINOS || OPT_UNIX ErrorCode RGZipFile::Create(XGFileSpecifier *) { #error TO_BE_PORTED } #endif // --------------------------------------------------------------------------- /// LocateSubFile // bool RGZipFile::LocateSubFile(const char *szFileName) { bool valid = false; MyAssert(_unzf != NULL); valid = unzLocateFile(_unzf, szFileName, 0) != UNZ_END_OF_LIST_OF_FILE; if(valid) unzOpenCurrentFile(_unzf); return valid; } // --------------------------------------------------------------------------- /// CreateSubFile // void RGZipFile::CreateSubFile(const char *szFileName, ZipCompressionMethod_t method) { zip_fileinfo info; RGDateTime date; MyAssert(_zf != NULL); date.Now(); info.tmz_date.tm_sec = date.second(); info.tmz_date.tm_min = date.minute(); info.tmz_date.tm_hour = date.hour(); info.tmz_date.tm_mday = date.day(); info.tmz_date.tm_mon = date.month(); info.tmz_date.tm_year = date.year(); info.dosDate = 0; // if dos_date == 0, tmu_date is used info.internal_fa = 0; // internal file attributes 2 bytes info.external_fa = 0; // external file attributes 4 bytes if(method == kDefaultCompression) zipOpenNewFileInZip(_zf, szFileName, &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION); else zipOpenNewFileInZip(_zf, szFileName, &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_NO_COMPRESSION); } // --------------------------------------------------------------------------- /// CloseSubFile // void RGZipFile::CloseSubFile() { if(_zf != NULL) zipCloseFileInZip(_zf); else if(_unzf != NULL) unzCloseCurrentFile(_unzf); } // --------------------------------------------------------------------------- /// Close // ErrorCode RGZipFile::Close() { if(_unzf != NULL) unzClose(_unzf); if(_zf != NULL) zipClose(_zf, "GardenSketch"); return KYAAFNoError; } // --------------------------------------------------------------------------- /// Read // long RGZipFile::Read(long len, void *buf) { MyAssert(_unzf != NULL); return unzReadCurrentFile(_unzf, buf, len); } // --------------------------------------------------------------------------- /// Write // long RGZipFile::Write(long len, const void *buf) { MyAssert(_zf != NULL); return zipWriteInFileInZip(_zf, buf, len); } // --------------------------------------------------------------------------- /// GetFilePos // long RGZipFile::GetFilePos() { MyAssert(_unzf != NULL); return unzGetOffset(_unzf); } // --------------------------------------------------------------------------- /// SetFilePos // ErrorCode RGZipFile::SetFilePos(long pos) { MyAssert(_unzf != NULL); unzSetOffset (_unzf, pos); } --- NEW FILE: RGZipFile.h --- /********************************************************************** RGZipFile class Copyright 2004 by Jeffrey Bedell Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Neither the name of the copyright holder or other contributors may be used to promote products derived from this software without specific written prior permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Contact: je...@re... **********************************************************************/ #ifndef YAAF_RGZIPFILE #define YAAF_RGZIPFILE #include "zip.h" #include "unzip.h" #include "XGFile.h" using namespace yaaf; typedef enum { kNoCompression, kDefaultCompression } ZipCompressionMethod_t; class RGZipFile : public yaaf::XGFile { public: RGZipFile(); virtual ~RGZipFile(); virtual ErrorCode Open(XGFileSpecifier *, bool rwflag); #if OPT_MACOS virtual ErrorCode Create(XGFileSpecifier *, OSType = 'bina', OSType = 'yaaf'); #endif #if OPT_WINOS || OPT_UNIX virtual ErrorCode Create(XGFileSpecifier *); #endif virtual ErrorCode Close(); virtual long Read(long, void *); virtual long Write(long, const void *); virtual long GetFilePos(); virtual ErrorCode SetFilePos(long); bool LocateSubFile(const char *szFileName); void CreateSubFile(const char *szFileName, ZipCompressionMethod_t method = kDefaultCompression); void CloseSubFile(); private: zipFile _zf; unzFile _unzf; bool _writable; }; #endif // YAAF_RGZIPFILE |