X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..5e6b515abaad0dd8b8ad3baea5e94311ec538ab7:/src/common/zipstrm.cpp?ds=sidebyside diff --git a/src/common/zipstrm.cpp b/src/common/zipstrm.cpp index fb191d35c3..d01d464df2 100644 --- a/src/common/zipstrm.cpp +++ b/src/common/zipstrm.cpp @@ -6,7 +6,7 @@ // Licence: wxWindows Licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "zipstrm.h" #endif @@ -24,6 +24,7 @@ #include "wx/stream.h" #include "wx/wfstream.h" #include "wx/zipstrm.h" +#include "wx/utils.h" /* Not the right solution (paths in makefiles) but... */ #ifdef __BORLANDC__ @@ -39,15 +40,16 @@ wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file m_Pos = 0; m_Size = 0; - m_Archive = (void*) unzOpen(archive.mb_str()); + m_Archive = (void*) unzOpen(archive.mb_str(wxConvFile)); if (m_Archive == NULL) { - m_lasterror = wxStream_READ_ERR; + m_lasterror = wxSTREAM_READ_ERROR; return; } - if (unzLocateFile((unzFile)m_Archive, file.mb_str(), 0) != UNZ_OK) + // TODO what encoding does ZIP use? + if (unzLocateFile((unzFile)m_Archive, file.ToAscii(), 0) != UNZ_OK) { - m_lasterror = wxStream_READ_ERR; + m_lasterror = wxSTREAM_READ_ERROR; return; } @@ -55,7 +57,7 @@ wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) { - m_lasterror = wxStream_READ_ERR; + m_lasterror = wxSTREAM_READ_ERROR; return; } m_Size = (size_t)zinfo.uncompressed_size; @@ -89,7 +91,7 @@ size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize) if ( m_Pos >= (off_t)m_Size ) { - m_lasterror = wxStream_EOF; + m_lasterror = wxSTREAM_EOF; return 0; } @@ -106,10 +108,14 @@ size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize) off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode) { + // NB: since ZIP files don't natively support seeking, we have to + // implement a brute force workaround -- reading all the data + // between current and the new position (or between beginning of + // the file and new position...) + off_t nextpos; - void *buf; - switch (mode) + switch ( mode ) { case wxFromCurrent : nextpos = seek + m_Pos; break; case wxFromStart : nextpos = seek; break; @@ -117,23 +123,33 @@ off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode) default : nextpos = m_Pos; break; /* just to fool compiler, never happens */ } - // cheated seeking : - if (nextpos > m_Pos) + size_t toskip; + if ( nextpos > m_Pos ) { - buf = malloc(nextpos - m_Pos); - unzReadCurrentFile((unzFile)m_Archive, buf, nextpos - m_Pos); - free(buf); + toskip = nextpos - m_Pos; } - else if (nextpos < m_Pos) { + else + { unzCloseCurrentFile((unzFile)m_Archive); if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) { - m_lasterror = wxStream_READ_ERR; + m_lasterror = wxSTREAM_READ_ERROR; return m_Pos; } - buf = malloc(nextpos); - unzReadCurrentFile((unzFile)m_Archive, buf, nextpos); - free(buf); + toskip = nextpos; + } + + if ( toskip > 0 ) + { + const size_t BUFSIZE = 4096; + size_t sz; + char buffer[BUFSIZE]; + while ( toskip > 0 ) + { + sz = wxMin(toskip, BUFSIZE); + unzReadCurrentFile((unzFile)m_Archive, buffer, sz); + toskip -= sz; + } } m_Pos = nextpos;