]>
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());
44 if (m_Archive
== NULL
)
46 m_lasterror
= wxSTREAM_READ_ERROR
;
49 if (unzLocateFile((unzFile
)m_Archive
, file
.mb_str(), 0) != UNZ_OK
)
51 m_lasterror
= wxSTREAM_READ_ERROR
;
55 unzGetCurrentFileInfo((unzFile
)m_Archive
, &zinfo
, (char*) NULL
, 0, (void*) NULL
, 0, (char*) NULL
, 0);
57 if (unzOpenCurrentFile((unzFile
)m_Archive
) != UNZ_OK
)
59 m_lasterror
= wxSTREAM_READ_ERROR
;
62 m_Size
= (size_t)zinfo
.uncompressed_size
;
67 wxZipInputStream::~wxZipInputStream()
72 unzCloseCurrentFile((unzFile
)m_Archive
);
73 unzClose((unzFile
)m_Archive
);
77 bool wxZipInputStream::Eof() const
79 wxASSERT_MSG( m_Pos
<= (off_t
)m_Size
,
80 _T("wxZipInputStream: invalid current position") );
82 return m_Pos
>= (off_t
)m_Size
;
86 size_t wxZipInputStream::OnSysRead(void *buffer
, size_t bufsize
)
88 wxASSERT_MSG( m_Pos
<= (off_t
)m_Size
,
89 _T("wxZipInputStream: invalid current position") );
91 if ( m_Pos
>= (off_t
)m_Size
)
93 m_lasterror
= wxSTREAM_EOF
;
97 if (m_Pos
+ bufsize
> m_Size
)
98 bufsize
= m_Size
- m_Pos
;
100 unzReadCurrentFile((unzFile
)m_Archive
, buffer
, bufsize
);
108 off_t
wxZipInputStream::OnSysSeek(off_t seek
, wxSeekMode mode
)
110 // NB: since ZIP files don't natively support seeking, we have to
111 // implement a brute force workaround -- reading all the data
112 // between current and the new position (or between beginning of
113 // the file and new position...)
119 case wxFromCurrent
: nextpos
= seek
+ m_Pos
; break;
120 case wxFromStart
: nextpos
= seek
; break;
121 case wxFromEnd
: nextpos
= m_Size
- 1 + seek
; break;
122 default : nextpos
= m_Pos
; break; /* just to fool compiler, never happens */
126 if ( nextpos
> m_Pos
)
128 toskip
= nextpos
- m_Pos
;
132 unzCloseCurrentFile((unzFile
)m_Archive
);
133 if (unzOpenCurrentFile((unzFile
)m_Archive
) != UNZ_OK
)
135 m_lasterror
= wxSTREAM_READ_ERROR
;
143 const size_t BUFSIZE
= 4096;
145 char buffer
[BUFSIZE
];
148 sz
= wxMin(toskip
, BUFSIZE
);
149 unzReadCurrentFile((unzFile
)m_Archive
, buffer
, sz
);
159 // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB