added ZIP classes by M.J.Wetherell (patch 1030239)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@30436 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2004-11-10 23:58:38 +00:00
parent eabd333355
commit 00375592f9
16 changed files with 6531 additions and 167 deletions

353
include/wx/archive.h Normal file
View File

@@ -0,0 +1,353 @@
/////////////////////////////////////////////////////////////////////////////
// Name: archive.h
// Purpose: Streams for archive formats
// Author: Mike Wetherell
// RCS-ID: $Id$
// Copyright: (c) 2004 Mike Wetherell
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_ARCHIVE_H__
#define _WX_ARCHIVE_H__
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma interface "archive.h"
#endif
#include "wx/defs.h"
#if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
#include "wx/stream.h"
#include "wx/filename.h"
/////////////////////////////////////////////////////////////////////////////
// wxArchiveNotifier
class WXDLLIMPEXP_BASE wxArchiveNotifier
{
public:
virtual ~wxArchiveNotifier() { }
virtual void OnEntryUpdated(class wxArchiveEntry& entry) = 0;
};
/////////////////////////////////////////////////////////////////////////////
// wxArchiveEntry
//
// Holds an entry's meta data, such as filename and timestamp.
class WXDLLIMPEXP_BASE wxArchiveEntry : public wxObject
{
public:
virtual ~wxArchiveEntry() { }
virtual wxDateTime GetDateTime() const = 0;
virtual wxFileOffset GetSize() const = 0;
virtual wxFileOffset GetOffset() const = 0;
virtual bool IsDir() const = 0;
virtual bool IsReadOnly() const = 0;
virtual wxString GetInternalName() const = 0;
virtual wxPathFormat GetInternalFormat() const = 0;
virtual wxString GetName(wxPathFormat format = wxPATH_NATIVE) const = 0;
virtual void SetDateTime(const wxDateTime& dt) = 0;
virtual void SetSize(wxFileOffset size) = 0;
virtual void SetIsDir(bool isDir = true) = 0;
virtual void SetIsReadOnly(bool isReadOnly = true) = 0;
virtual void SetName(const wxString& name,
wxPathFormat format = wxPATH_NATIVE) = 0;
wxArchiveEntry *Clone() const { return DoClone(); }
void SetNotifier(wxArchiveNotifier& notifier);
virtual void UnsetNotifier() { m_notifier = NULL; }
protected:
wxArchiveEntry() : m_notifier(NULL) { }
virtual void SetOffset(wxFileOffset offset) = 0;
virtual wxArchiveEntry* DoClone() const = 0;
wxArchiveNotifier *GetNotifier() const { return m_notifier; }
wxArchiveEntry& operator=(const wxArchiveEntry& entry);
private:
wxArchiveNotifier *m_notifier;
DECLARE_ABSTRACT_CLASS(wxArchiveEntry)
};
/////////////////////////////////////////////////////////////////////////////
// wxArchiveInputStream
//
// GetNextEntry() returns an wxArchiveEntry object containing the meta-data
// for the next entry in the archive (and gives away ownership). Reading from
// the wxArchiveInputStream then returns the entry's data. Eof() becomes true
// after an attempt has been made to read past the end of the entry's data.
//
// When there are no more entries, GetNextEntry() returns NULL and sets Eof().
class WXDLLIMPEXP_BASE wxArchiveInputStream : public wxFilterInputStream
{
public:
typedef wxArchiveEntry entry_type;
virtual ~wxArchiveInputStream() { }
virtual bool OpenEntry(wxArchiveEntry& entry) = 0;
virtual bool CloseEntry() = 0;
wxArchiveEntry *GetNextEntry() { return DoGetNextEntry(); }
virtual char Peek() { return wxInputStream::Peek(); }
protected:
wxArchiveInputStream(wxInputStream& stream, wxMBConv& conv);
virtual wxArchiveEntry *DoGetNextEntry() = 0;
wxMBConv& GetConv() const { return m_conv; }
private:
wxMBConv& m_conv;
};
/////////////////////////////////////////////////////////////////////////////
// wxArchiveOutputStream
//
// PutNextEntry is used to create a new entry in the output archive, then
// the entry's data is written to the wxArchiveOutputStream.
//
// Only one entry can be open for output at a time; another call to
// PutNextEntry closes the current entry and begins the next.
//
// The overload 'bool PutNextEntry(wxArchiveEntry *entry)' takes ownership
// of the entry object.
class WXDLLIMPEXP_BASE wxArchiveOutputStream : public wxFilterOutputStream
{
public:
virtual ~wxArchiveOutputStream() { }
virtual bool PutNextEntry(wxArchiveEntry *entry) = 0;
virtual bool PutNextEntry(const wxString& name,
const wxDateTime& dt = wxDateTime::Now(),
wxFileOffset size = wxInvalidOffset) = 0;
virtual bool PutNextDirEntry(const wxString& name,
const wxDateTime& dt = wxDateTime::Now()) = 0;
virtual bool CopyEntry(wxArchiveEntry *entry,
wxArchiveInputStream& stream) = 0;
virtual bool CopyArchiveMetaData(wxArchiveInputStream& stream) = 0;
virtual bool CloseEntry() = 0;
virtual bool Close() = 0;
protected:
wxArchiveOutputStream(wxOutputStream& stream, wxMBConv& conv);
wxMBConv& GetConv() const { return m_conv; }
private:
wxMBConv& m_conv;
};
/////////////////////////////////////////////////////////////////////////////
// wxArchiveClassFactory
//
// A wxArchiveClassFactory instance for a particular archive type allows
// the creation of the other classes that may be needed.
class WXDLLIMPEXP_BASE wxArchiveClassFactory : public wxObject
{
public:
virtual ~wxArchiveClassFactory() { }
wxArchiveEntry *NewEntry() const
{ return DoNewEntry(); }
wxArchiveInputStream *NewStream(wxInputStream& stream) const
{ return DoNewStream(stream); }
wxArchiveOutputStream *NewStream(wxOutputStream& stream) const
{ return DoNewStream(stream); }
virtual wxString GetInternalName(
const wxString& name,
wxPathFormat format = wxPATH_NATIVE) const = 0;
void SetConv(wxMBConv& conv) { m_pConv = &conv; }
wxMBConv& GetConv() const { return *m_pConv; }
protected:
virtual wxArchiveEntry *DoNewEntry() const = 0;
virtual wxArchiveInputStream *DoNewStream(wxInputStream& stream) const = 0;
virtual wxArchiveOutputStream *DoNewStream(wxOutputStream& stream) const = 0;
wxArchiveClassFactory() : m_pConv(&wxConvLocal) { }
wxArchiveClassFactory& operator=(const wxArchiveClassFactory& WXUNUSED(f))
{ return *this; }
private:
wxMBConv *m_pConv;
DECLARE_ABSTRACT_CLASS(wxArchiveClassFactory)
};
/////////////////////////////////////////////////////////////////////////////
// wxArchiveIterator
//
// An input iterator that can be used to transfer an archive's catalog to
// a container.
#if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
#include <iterator>
#include <utility>
template <class X, class Y>
void WXDLLIMPEXP_BASE _wxSetArchiveIteratorValue(
X& val, Y entry, void *WXUNUSED(d))
{
val = X(entry);
}
template <class X, class Y, class Z>
void WXDLLIMPEXP_BASE _wxSetArchiveIteratorValue(
std::pair<X, Y>& val, Z entry, Z WXUNUSED(d))
{
val = std::make_pair(X(entry->GetInternalName()), Y(entry));
}
#if defined _MSC_VER && _MSC_VER < 1300
template <class Arc, class T = Arc::entry_type*>
#else
template <class Arc, class T = typename Arc::entry_type*>
#endif
class WXDLLIMPEXP_BASE wxArchiveIterator
{
public:
typedef std::input_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
wxArchiveIterator() : m_rep(NULL) { }
wxArchiveIterator(Arc& arc) {
typename Arc::entry_type* entry = arc.GetNextEntry();
m_rep = entry ? new Rep(arc, entry) : NULL;
}
wxArchiveIterator(const wxArchiveIterator& it) : m_rep(it.m_rep) {
if (m_rep)
m_rep->AddRef();
}
~wxArchiveIterator() {
if (m_rep)
m_rep->UnRef();
}
const T& operator *() const {
return m_rep->GetValue();
}
const T* operator ->() const {
return &**this;
}
wxArchiveIterator& operator =(const wxArchiveIterator& it) {
if (it.m_rep)
it.m_rep.AddRef();
if (m_rep)
m_rep.UnRef();
m_rep = it.m_rep;
return *this;
}
wxArchiveIterator& operator ++() {
m_rep = m_rep->Next();
return *this;
}
wxArchiveIterator operator ++(int) {
wxArchiveIterator it(*this);
++(*this);
return it;
}
friend bool operator ==(const wxArchiveIterator& i,
const wxArchiveIterator& j) {
return i.m_rep == j.m_rep;
}
friend bool operator !=(const wxArchiveIterator& i,
const wxArchiveIterator& j) {
return !(i == j);
}
private:
class Rep {
Arc& m_arc;
typename Arc::entry_type* m_entry;
T m_value;
int m_ref;
public:
Rep(Arc& arc, typename Arc::entry_type* entry)
: m_arc(arc), m_entry(entry), m_value(), m_ref(1) { }
~Rep()
{ delete m_entry; }
void AddRef() {
m_ref++;
}
void UnRef() {
if (--m_ref == 0)
delete this;
}
Rep *Next() {
typename Arc::entry_type* entry = m_arc.GetNextEntry();
if (!entry) {
UnRef();
return NULL;
}
if (m_ref > 1) {
m_ref--;
return new Rep(m_arc, entry);
}
delete m_entry;
m_entry = entry;
m_value = T();
return this;
}
const T& GetValue() {
if (m_entry) {
_wxSetArchiveIteratorValue(m_value, m_entry, m_entry);
m_entry = NULL;
}
return m_value;
}
} *m_rep;
};
typedef wxArchiveIterator<wxArchiveInputStream> wxArchiveIter;
typedef wxArchiveIterator<wxArchiveInputStream,
std::pair<wxString, wxArchiveEntry*> > wxArchivePairIter;
#endif // wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
#endif // wxUSE_STREAMS
#endif // _WX_ARCHIVE_H__

View File

@@ -37,12 +37,13 @@ class WXDLLIMPEXP_BASE wxZipFSHandler : public wxFileSystemHandler
private:
// these vars are used by FindFirst/Next:
void *m_Archive;
class wxZipInputStream *m_Archive;
wxString m_Pattern, m_BaseDir, m_ZipFile;
bool m_AllowDirs, m_AllowFiles;
wxLongToLongHashMap *m_DirsFound;
wxString DoFind();
void CloseArchive(class wxZipInputStream *archive);
DECLARE_NO_COPY_CLASS(wxZipFSHandler)
};

View File

@@ -1,13 +1,14 @@
/////////////////////////////////////////////////////////////////////////////
// Name: zipstream.h
// Purpose: wxZipInputStream for reading files from ZIP archive
// Author: Vaclav Slavik
// Copyright: (c) 1999 Vaclav Slavik
// Name: zipstrm.h
// Purpose: Streams for Zip files
// Author: Mike Wetherell
// RCS-ID: $Id$
// Copyright: (c) Mike Wetherell
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef __ZIPSTREAM_H__
#define __ZIPSTREAM_H__
#ifndef _WX_WXZIPSTREAM_H__
#define _WX_WXZIPSTREAM_H__
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma interface "zipstrm.h"
@@ -15,47 +16,507 @@
#include "wx/defs.h"
#if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
#if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
#include "wx/stream.h"
//--------------------------------------------------------------------------------
// wxZipInputStream
// This class is input stream from ZIP archive. The archive
// must be local file (accessible via FILE*)
//--------------------------------------------------------------------------------
#include "wx/archive.h"
#include "wx/hashmap.h"
#include "wx/filename.h"
class WXDLLIMPEXP_BASE wxZipInputStream : public wxInputStream
/////////////////////////////////////////////////////////////////////////////
// constants
// Compression Method, only 0 (store) and 8 (deflate) are supported here
//
enum wxZipMethod
{
wxZIP_METHOD_STORE,
wxZIP_METHOD_SHRINK,
wxZIP_METHOD_REDUCE1,
wxZIP_METHOD_REDUCE2,
wxZIP_METHOD_REDUCE3,
wxZIP_METHOD_REDUCE4,
wxZIP_METHOD_IMPLODE,
wxZIP_METHOD_TOKENIZE,
wxZIP_METHOD_DEFLATE,
wxZIP_METHOD_DEFLATE64,
wxZIP_METHOD_BZIP2 = 12,
wxZIP_METHOD_DEFAULT = 0xffff
};
// Originating File-System.
//
// These are Pkware's values. Note that Info-zip disagree on some of them,
// most notably NTFS.
//
enum wxZipSystem
{
wxZIP_SYSTEM_MSDOS,
wxZIP_SYSTEM_AMIGA,
wxZIP_SYSTEM_OPENVMS,
wxZIP_SYSTEM_UNIX,
wxZIP_SYSTEM_VM_CMS,
wxZIP_SYSTEM_ATARI_ST,
wxZIP_SYSTEM_OS2_HPFS,
wxZIP_SYSTEM_MACINTOSH,
wxZIP_SYSTEM_Z_SYSTEM,
wxZIP_SYSTEM_CPM,
wxZIP_SYSTEM_WINDOWS_NTFS,
wxZIP_SYSTEM_MVS,
wxZIP_SYSTEM_VSE,
wxZIP_SYSTEM_ACORN_RISC,
wxZIP_SYSTEM_VFAT,
wxZIP_SYSTEM_ALTERNATE_MVS,
wxZIP_SYSTEM_BEOS,
wxZIP_SYSTEM_TANDEM,
wxZIP_SYSTEM_OS_400
};
// Dos/Win file attributes
//
enum wxZipAttributes
{
wxZIP_A_RDONLY = 0x01,
wxZIP_A_HIDDEN = 0x02,
wxZIP_A_SYSTEM = 0x04,
wxZIP_A_SUBDIR = 0x10,
wxZIP_A_ARCH = 0x20,
wxZIP_A_MASK = 0x37
};
// Values for the flags field in the zip headers
//
enum wxZipFlags
{
wxZIP_ENCRYPTED = 0x0001,
wxZIP_DEFLATE_NORMAL = 0x0000, // normal compression
wxZIP_DEFLATE_EXTRA = 0x0002, // extra compression
wxZIP_DEFLATE_FAST = 0x0004, // fast compression
wxZIP_DEFLATE_SUPERFAST = 0x0006, // superfast compression
wxZIP_DEFLATE_MASK = 0x0006,
wxZIP_SUMS_FOLLOW = 0x0008, // crc and sizes come after the data
wxZIP_ENHANCED = 0x0010,
wxZIP_PATCH = 0x0020,
wxZIP_STRONG_ENC = 0x0040,
wxZIP_UNUSED = 0x0F80,
wxZIP_RESERVED = 0xF000
};
// Forward decls
//
class WXDLLIMPEXP_BASE wxZipEntry;
class WXDLLIMPEXP_BASE wxZipInputStream;
/////////////////////////////////////////////////////////////////////////////
// wxZipNotifier
class WXDLLIMPEXP_BASE wxZipNotifier
{
public:
wxZipInputStream(const wxString& archive, const wxString& file);
// archive is name of .zip archive, file is name of file to be extracted.
// Remember that archive must be local file accesible via fopen, fread functions!
~wxZipInputStream();
virtual ~wxZipNotifier() { }
virtual wxFileOffset GetLength() const {return m_Size;}
virtual bool Eof() const;
virtual void OnEntryUpdated(wxZipEntry& entry) = 0;
};
/////////////////////////////////////////////////////////////////////////////
// Zip Entry - holds the meta data for a file in the zip
class WXDLLIMPEXP_BASE wxZipEntry : public wxArchiveEntry
{
public:
wxZipEntry(const wxString& name = wxEmptyString,
const wxDateTime& dt = wxDateTime::Now(),
wxFileOffset size = wxInvalidOffset);
virtual ~wxZipEntry();
wxZipEntry(const wxZipEntry& entry);
wxZipEntry& operator=(const wxZipEntry& entry);
// Get accessors
wxDateTime GetDateTime() const { return m_DateTime; }
wxFileOffset GetSize() const { return m_Size; }
wxFileOffset GetOffset() const { return m_Offset; }
wxString GetInternalName() const { return m_Name; }
int GetMethod() const { return m_Method; }
int GetFlags() const { return m_Flags; }
wxUint32 GetCrc() const { return m_Crc; }
wxFileOffset GetCompressedSize() const { return m_CompressedSize; }
int GetSystemMadeBy() const { return m_SystemMadeBy; }
wxString GetComment() const { return m_Comment; }
wxUint32 GetExternalAttributes() const { return m_ExternalAttributes; }
wxPathFormat GetInternalFormat() const { return wxPATH_UNIX; }
int GetMode() const;
const char *GetLocalExtra() const;
size_t GetLocalExtraLen() const;
const char *GetExtra() const;
size_t GetExtraLen() const;
wxString GetName(wxPathFormat format = wxPATH_NATIVE) const;
// is accessors
inline bool IsDir() const;
inline bool IsText() const;
inline bool IsReadOnly() const;
inline bool IsMadeByUnix() const;
// set accessors
void SetDateTime(const wxDateTime& dt) { m_DateTime = dt; }
void SetSize(wxFileOffset size) { m_Size = size; }
void SetMethod(int method) { m_Method = method; }
void SetComment(const wxString& comment) { m_Comment = comment; }
void SetExternalAttributes(wxUint32 attr ) { m_ExternalAttributes = attr; }
void SetSystemMadeBy(int system);
void SetMode(int mode);
void SetExtra(const char *extra, size_t len);
void SetLocalExtra(const char *extra, size_t len);
inline void SetName(const wxString& name,
wxPathFormat format = wxPATH_NATIVE);
static wxString GetInternalName(const wxString& name,
wxPathFormat format = wxPATH_NATIVE,
bool *pIsDir = NULL);
// set is accessors
void SetIsDir(bool isDir = true);
inline void SetIsReadOnly(bool isReadOnly = true);
inline void SetIsText(bool isText = true);
wxZipEntry *Clone() const { return ZipClone(); }
void SetNotifier(wxZipNotifier& notifier);
void UnsetNotifier();
protected:
virtual size_t OnSysRead(void *buffer, size_t bufsize);
virtual wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
virtual wxFileOffset OnSysTell() const {return m_Pos;}
// Internal attributes
enum { TEXT_ATTR = 1 };
// protected Get accessors
int GetVersionNeeded() const { return m_VersionNeeded; }
wxFileOffset GetKey() const { return m_Key; }
int GetVersionMadeBy() const { return m_VersionMadeBy; }
int GetDiskStart() const { return m_DiskStart; }
int GetInternalAttributes() const { return m_InternalAttributes; }
void SetVersionNeeded(int version) { m_VersionNeeded = version; }
void SetOffset(wxFileOffset offset) { m_Offset = offset; }
void SetFlags(int flags) { m_Flags = flags; }
void SetVersionMadeBy(int version) { m_VersionMadeBy = version; }
void SetCrc(wxUint32 crc) { m_Crc = crc; }
void SetCompressedSize(wxFileOffset size) { m_CompressedSize = size; }
void SetKey(wxFileOffset offset) { m_Key = offset; }
void SetDiskStart(int start) { m_DiskStart = start; }
void SetInternalAttributes(int attr) { m_InternalAttributes = attr; }
virtual wxZipEntry *ZipClone() const { return new wxZipEntry(*this); }
void Notify();
private:
wxFileOffset m_Size;
wxFileOffset m_Pos;
wxArchiveEntry* DoClone() const { return ZipClone(); }
// this void* is handle of archive . I'm sorry it is void and not proper
// type but I don't want to make unzip.h header public.
void *m_Archive;
size_t ReadLocal(wxInputStream& stream, wxMBConv& conv);
size_t WriteLocal(wxOutputStream& stream, wxMBConv& conv) const;
size_t ReadCentral(wxInputStream& stream, wxMBConv& conv);
size_t WriteCentral(wxOutputStream& stream, wxMBConv& conv) const;
size_t ReadDescriptor(wxInputStream& stream);
size_t WriteDescriptor(wxOutputStream& stream, wxUint32 crc,
wxFileOffset compressedSize, wxFileOffset size);
wxUint8 m_SystemMadeBy; // one of enum wxZipSystem
wxUint8 m_VersionMadeBy; // major * 10 + minor
wxUint16 m_VersionNeeded; // ver needed to extract (20 i.e. v2.0)
wxUint16 m_Flags;
wxUint16 m_Method; // compression method (one of wxZipMethod)
wxDateTime m_DateTime;
wxUint32 m_Crc;
wxFileOffset m_CompressedSize;
wxFileOffset m_Size;
wxString m_Name; // in internal format
wxFileOffset m_Key; // the original offset for copied entries
wxFileOffset m_Offset; // file offset of the entry
wxString m_Comment;
wxUint16 m_DiskStart; // for multidisk archives, not unsupported
wxUint16 m_InternalAttributes; // bit 0 set for text files
wxUint32 m_ExternalAttributes; // system specific depends on SystemMadeBy
class wxZipMemory *m_Extra;
class wxZipMemory *m_LocalExtra;
wxZipNotifier *m_zipnotifier;
class wxZipWeakLinks *m_backlink;
friend class wxZipInputStream;
friend class wxZipOutputStream;
DECLARE_DYNAMIC_CLASS(wxZipEntry)
};
/////////////////////////////////////////////////////////////////////////////
// wxZipOutputStream
WX_DECLARE_LIST_WITH_DECL(wxZipEntry, _wxZipEntryList, class WXDLLIMPEXP_BASE);
class WXDLLIMPEXP_BASE wxZipOutputStream : public wxArchiveOutputStream
{
public:
wxZipOutputStream(wxOutputStream& stream,
int level = -1,
wxMBConv& conv = wxConvLocal);
virtual ~wxZipOutputStream();
bool PutNextEntry(wxZipEntry *entry) { return DoCreate(entry); }
bool PutNextEntry(const wxString& name,
const wxDateTime& dt = wxDateTime::Now(),
wxFileOffset size = wxInvalidOffset);
bool PutNextDirEntry(const wxString& name,
const wxDateTime& dt = wxDateTime::Now());
bool CopyEntry(wxZipEntry *entry, wxZipInputStream& inputStream);
bool CopyArchiveMetaData(wxZipInputStream& inputStream);
void Sync();
bool CloseEntry();
bool Close();
void SetComment(const wxString& comment) { m_Comment = comment; }
int GetLevel() const { return m_level; }
void SetLevel(int level);
protected:
virtual size_t OnSysWrite(const void *buffer, size_t size);
virtual wxFileOffset OnSysTell() const { return m_entrySize; }
struct Buffer { const char *m_data; size_t m_size; };
virtual wxOutputStream *OpenCompressor(wxOutputStream& stream,
wxZipEntry& entry,
const Buffer bufs[]);
virtual bool CloseCompressor(wxOutputStream *comp);
bool IsParentSeekable() const { return m_offsetAdjustment
!= wxInvalidOffset; }
private:
bool PutNextEntry(wxArchiveEntry *entry);
bool CopyEntry(wxArchiveEntry *entry, wxArchiveInputStream& stream);
bool CopyArchiveMetaData(wxArchiveInputStream& stream);
bool IsOpened() const { return m_comp || m_pending; }
bool DoCreate(wxZipEntry *entry, bool raw = false);
void CreatePendingEntry(const void *buffer, size_t size);
void CreatePendingEntry();
class wxStoredOutputStream *m_store;
class wxZlibOutputStream2 *m_deflate;
class wxZipStreamLink *m_backlink;
_wxZipEntryList m_entries;
char *m_initialData;
size_t m_initialSize;
wxZipEntry *m_pending;
bool m_raw;
wxFileOffset m_headerOffset;
size_t m_headerSize;
wxFileOffset m_entrySize;
wxUint32 m_crcAccumulator;
wxOutputStream *m_comp;
int m_level;
wxFileOffset m_offsetAdjustment;
wxString m_Comment;
DECLARE_NO_COPY_CLASS(wxZipOutputStream)
};
/////////////////////////////////////////////////////////////////////////////
// wxZipInputStream
class WXDLLIMPEXP_BASE wxZipInputStream : public wxArchiveInputStream
{
public:
typedef wxZipEntry entry_type;
wxZipInputStream(wxInputStream& stream, wxMBConv& conv = wxConvLocal);
wxZipInputStream(const wxString& archive, const wxString& file);
virtual ~wxZipInputStream();
bool OpenEntry(wxZipEntry& entry) { return DoOpen(&entry); }
bool CloseEntry();
wxZipEntry *GetNextEntry();
wxString GetComment();
int GetTotalEntries();
virtual wxFileOffset GetLength() const { return m_entry.GetSize(); }
protected:
size_t OnSysRead(void *buffer, size_t size);
wxFileOffset OnSysTell() const { return m_decomp ? m_decomp->TellI() : 0; }
wxFileOffset OnSysSeek(wxFileOffset seek, wxSeekMode mode);
virtual wxInputStream *OpenDecompressor(wxInputStream& stream);
virtual bool CloseDecompressor(wxInputStream *decomp);
private:
void Init();
wxInputStream& OpenFile(const wxString& archive);
wxArchiveEntry *DoGetNextEntry() { return GetNextEntry(); }
bool OpenEntry(wxArchiveEntry& entry);
wxStreamError ReadLocal(bool readEndRec = false);
wxStreamError ReadCentral();
wxUint32 ReadSignature();
bool FindEndRecord();
bool LoadEndRecord();
bool AtHeader() const { return m_headerSize == 0; }
bool AfterHeader() const { return m_headerSize > 0 && !m_decomp; }
bool IsOpened() const { return m_decomp != NULL; }
wxZipStreamLink *MakeLink(wxZipOutputStream *out);
bool DoOpen(wxZipEntry *entry = NULL, bool raw = false);
bool OpenDecompressor(bool raw = false);
class wxStoredInputStream *m_store;
class wxZlibInputStream2 *m_inflate;
class wxRawInputStream *m_rawin;
class wxFFileInputStream *m_ffile;
wxZipEntry m_entry;
bool m_raw;
size_t m_headerSize;
wxUint32 m_crcAccumulator;
wxInputStream *m_decomp;
bool m_parentSeekable;
class wxZipWeakLinks *m_weaklinks;
class wxZipStreamLink *m_streamlink;
wxFileOffset m_offsetAdjustment;
wxFileOffset m_position;
wxUint32 m_signature;
size_t m_TotalEntries;
wxString m_Comment;
friend bool wxZipOutputStream::CopyEntry(
wxZipEntry *entry, wxZipInputStream& inputStream);
friend bool wxZipOutputStream::CopyArchiveMetaData(
wxZipInputStream& inputStream);
DECLARE_NO_COPY_CLASS(wxZipInputStream)
};
#endif
// wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
/////////////////////////////////////////////////////////////////////////////
// wxZipClassFactory
class WXDLLIMPEXP_BASE wxZipClassFactory : public wxArchiveClassFactory
{
public:
wxZipEntry *NewEntry() const
{ return new wxZipEntry; }
wxZipInputStream *NewStream(wxInputStream& stream) const
{ return new wxZipInputStream(stream, GetConv()); }
wxZipOutputStream *NewStream(wxOutputStream& stream) const
{ return new wxZipOutputStream(stream, -1, GetConv()); }
wxString GetInternalName(const wxString& name,
wxPathFormat format = wxPATH_NATIVE) const
{ return wxZipEntry::GetInternalName(name, format); }
protected:
wxArchiveEntry *DoNewEntry() const
{ return NewEntry(); }
wxArchiveInputStream *DoNewStream(wxInputStream& stream) const
{ return NewStream(stream); }
wxArchiveOutputStream *DoNewStream(wxOutputStream& stream) const
{ return NewStream(stream); }
private:
DECLARE_DYNAMIC_CLASS(wxZipClassFactory)
};
/////////////////////////////////////////////////////////////////////////////
// Iterators
#if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
typedef wxArchiveIterator<wxZipInputStream> wxZipIter;
typedef wxArchiveIterator<wxZipInputStream,
std::pair<wxString, wxZipEntry*> > wxZipPairIter;
#endif
// __ZIPSTREAM_H__
/////////////////////////////////////////////////////////////////////////////
// wxZipEntry inlines
bool wxZipEntry::IsText() const
{
return (m_InternalAttributes & TEXT_ATTR) != 0;
}
bool wxZipEntry::IsDir() const
{
return (m_ExternalAttributes & wxZIP_A_SUBDIR) != 0;
}
bool wxZipEntry::IsReadOnly() const
{
return (m_ExternalAttributes & wxZIP_A_RDONLY) != 0;
}
bool wxZipEntry::IsMadeByUnix() const
{
const int pattern =
(1 << wxZIP_SYSTEM_OPENVMS) |
(1 << wxZIP_SYSTEM_UNIX) |
(1 << wxZIP_SYSTEM_ATARI_ST) |
(1 << wxZIP_SYSTEM_ACORN_RISC) |
(1 << wxZIP_SYSTEM_BEOS) | (1 << wxZIP_SYSTEM_TANDEM);
// note: some unix zippers put madeby = dos
return (m_SystemMadeBy == wxZIP_SYSTEM_MSDOS
&& (m_ExternalAttributes & ~0xFFFF))
|| ((pattern >> m_SystemMadeBy) & 1);
}
void wxZipEntry::SetIsText(bool isText)
{
if (isText)
m_InternalAttributes |= TEXT_ATTR;
else
m_InternalAttributes &= ~TEXT_ATTR;
}
void wxZipEntry::SetIsReadOnly(bool isReadOnly)
{
if (isReadOnly)
SetMode(GetMode() & ~0222);
else
SetMode(GetMode() | 0200);
}
void wxZipEntry::SetName(const wxString& name,
wxPathFormat format /*=wxPATH_NATIVE*/)
{
bool isDir;
m_Name = GetInternalName(name, format, &isDir);
SetIsDir(isDir);
}
#endif // wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
#endif // _WX_WXZIPSTREAM_H__