]>
git.saurik.com Git - wxWidgets.git/blob - src/common/zipstrm.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     input stream for ZIP archive access 
   4 // Author:      Vaclav Slavik 
   5 // Copyright:   (c) 1999 Vaclav Slavik 
   6 // Licence:     wxWindows Licence 
   7 ///////////////////////////////////////////////////////////////////////////// 
   9 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  10 #pragma implementation "zipstrm.h" 
  13 // For compilers that support precompilation, includes "wx.h". 
  14 #include "wx/wxprec.h" 
  20 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB 
  24 #include "wx/stream.h" 
  25 #include "wx/wfstream.h" 
  26 #include "wx/zipstrm.h" 
  29 /* Not the right solution (paths in makefiles) but... */ 
  31 #include "../common/unzip.h" 
  37 wxZipInputStream::wxZipInputStream(const wxString
& archive
, const wxString
& file
) : wxInputStream() 
  43     m_Archive 
= (void*) unzOpen(archive
.mb_str(wxConvFile
)); 
  44     if (m_Archive 
== NULL
) 
  46         m_lasterror 
= wxSTREAM_READ_ERROR
; 
  49     // TODO what encoding does ZIP use? 
  50     if (unzLocateFile((unzFile
)m_Archive
, file
.ToAscii(), 0) != UNZ_OK
) 
  52         m_lasterror 
= wxSTREAM_READ_ERROR
; 
  56     unzGetCurrentFileInfo((unzFile
)m_Archive
, &zinfo
, (char*) NULL
, 0, (void*) NULL
, 0, (char*) NULL
, 0); 
  58     if (unzOpenCurrentFile((unzFile
)m_Archive
) != UNZ_OK
) 
  60         m_lasterror 
= wxSTREAM_READ_ERROR
; 
  63     m_Size 
= (size_t)zinfo
.uncompressed_size
; 
  68 wxZipInputStream::~wxZipInputStream() 
  73             unzCloseCurrentFile((unzFile
)m_Archive
); 
  74         unzClose((unzFile
)m_Archive
); 
  78 bool wxZipInputStream::Eof() const 
  80     wxASSERT_MSG( m_Pos 
<= (off_t
)m_Size
, 
  81                   _T("wxZipInputStream: invalid current position") ); 
  83     return m_Pos 
>= (off_t
)m_Size
; 
  87 size_t wxZipInputStream::OnSysRead(void *buffer
, size_t bufsize
) 
  89     wxASSERT_MSG( m_Pos 
<= (off_t
)m_Size
, 
  90                   _T("wxZipInputStream: invalid current position") ); 
  92     if ( m_Pos 
>= (off_t
)m_Size 
) 
  94         m_lasterror 
= wxSTREAM_EOF
; 
  98     if (m_Pos 
+ bufsize 
> m_Size
) 
  99         bufsize 
= m_Size 
- m_Pos
; 
 101     unzReadCurrentFile((unzFile
)m_Archive
, buffer
, bufsize
); 
 109 off_t 
wxZipInputStream::OnSysSeek(off_t seek
, wxSeekMode mode
) 
 111     // NB: since ZIP files don't natively support seeking, we have to  
 112     //     implement a brute force workaround -- reading all the data 
 113     //     between current and the new position (or between beginning of  
 114     //     the file and new position...) 
 120         case wxFromCurrent 
: nextpos 
= seek 
+ m_Pos
; break; 
 121         case wxFromStart 
: nextpos 
= seek
; break; 
 122         case wxFromEnd 
: nextpos 
= m_Size 
- 1 + seek
; break; 
 123         default : nextpos 
= m_Pos
; break; /* just to fool compiler, never happens */ 
 127     if ( nextpos 
> m_Pos 
) 
 129         toskip 
= nextpos 
- m_Pos
; 
 133         unzCloseCurrentFile((unzFile
)m_Archive
); 
 134         if (unzOpenCurrentFile((unzFile
)m_Archive
) != UNZ_OK
) 
 136             m_lasterror 
= wxSTREAM_READ_ERROR
; 
 144         const size_t BUFSIZE 
= 4096; 
 146         char buffer
[BUFSIZE
]; 
 149             sz 
= wxMin(toskip
, BUFSIZE
); 
 150             unzReadCurrentFile((unzFile
)m_Archive
, buffer
, sz
); 
 160   // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB