]> git.saurik.com Git - wxWidgets.git/blame - src/common/zipstrm.cpp
make construct simpler
[wxWidgets.git] / src / common / zipstrm.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: zipstream.cpp
3// Purpose: input stream for ZIP archive access
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
6// Licence: wxWindows Licence
7/////////////////////////////////////////////////////////////////////////////
8
14f355c2 9#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
5279a24d 10#pragma implementation "zipstrm.h"
5526e819
VS
11#endif
12
d1af991f
RR
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
5526e819 15
d1af991f
RR
16#ifdef __BORLANDC__
17 #pragma hdrstop
5526e819
VS
18#endif
19
e90c1d2a 20#if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
d78b3d64 21
d1af991f
RR
22#include "wx/log.h"
23#include "wx/intl.h"
24#include "wx/stream.h"
25#include "wx/wfstream.h"
6001e347 26#include "wx/zipstrm.h"
03c0fc66 27#include "wx/utils.h"
ea4f5235
JS
28
29/* Not the right solution (paths in makefiles) but... */
30#ifdef __BORLANDC__
31#include "../common/unzip.h"
32#else
5526e819 33#include "unzip.h"
ea4f5235
JS
34#endif
35
5526e819 36
5526e819
VS
37wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file) : wxInputStream()
38{
39 unz_file_info zinfo;
40
41 m_Pos = 0;
42 m_Size = 0;
e83389ff 43 m_Archive = (void*) unzOpen(archive.mb_str(wxConvFile));
e90c1d2a 44 if (m_Archive == NULL)
d1af991f 45 {
2b5f62a0 46 m_lasterror = wxSTREAM_READ_ERROR;
5526e819
VS
47 return;
48 }
9d1f22e7
VS
49 // TODO what encoding does ZIP use?
50 if (unzLocateFile((unzFile)m_Archive, file.ToAscii(), 0) != UNZ_OK)
d1af991f 51 {
2b5f62a0 52 m_lasterror = wxSTREAM_READ_ERROR;
5526e819
VS
53 return;
54 }
e90c1d2a 55
d1af991f 56 unzGetCurrentFileInfo((unzFile)m_Archive, &zinfo, (char*) NULL, 0, (void*) NULL, 0, (char*) NULL, 0);
5526e819 57
e90c1d2a 58 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK)
d1af991f 59 {
2b5f62a0 60 m_lasterror = wxSTREAM_READ_ERROR;
5526e819
VS
61 return;
62 }
479cd5de 63 m_Size = (size_t)zinfo.uncompressed_size;
5526e819
VS
64}
65
66
67
68wxZipInputStream::~wxZipInputStream()
69{
e90c1d2a 70 if (m_Archive)
d1af991f 71 {
5526e819
VS
72 if (m_Size != 0)
73 unzCloseCurrentFile((unzFile)m_Archive);
74 unzClose((unzFile)m_Archive);
75 }
76}
77
f6bcfd97
BP
78bool wxZipInputStream::Eof() const
79{
80 wxASSERT_MSG( m_Pos <= (off_t)m_Size,
81 _T("wxZipInputStream: invalid current position") );
82
83 return m_Pos >= (off_t)m_Size;
84}
5526e819
VS
85
86
87size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize)
88{
f6bcfd97
BP
89 wxASSERT_MSG( m_Pos <= (off_t)m_Size,
90 _T("wxZipInputStream: invalid current position") );
91
92 if ( m_Pos >= (off_t)m_Size )
93 {
2b5f62a0 94 m_lasterror = wxSTREAM_EOF;
f6bcfd97
BP
95 return 0;
96 }
97
98 if (m_Pos + bufsize > m_Size)
99 bufsize = m_Size - m_Pos;
100
5526e819
VS
101 unzReadCurrentFile((unzFile)m_Archive, buffer, bufsize);
102 m_Pos += bufsize;
f6bcfd97 103
5526e819
VS
104 return bufsize;
105}
106
107
108
109off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode)
110{
03c0fc66
VS
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...)
115
5526e819 116 off_t nextpos;
5526e819 117
03c0fc66 118 switch ( mode )
d1af991f 119 {
5526e819
VS
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 */
124 }
125
999836aa 126 size_t toskip;
03c0fc66 127 if ( nextpos > m_Pos )
d1af991f 128 {
03c0fc66 129 toskip = nextpos - m_Pos;
5526e819 130 }
03c0fc66
VS
131 else
132 {
5526e819 133 unzCloseCurrentFile((unzFile)m_Archive);
e90c1d2a
VZ
134 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK)
135 {
2b5f62a0 136 m_lasterror = wxSTREAM_READ_ERROR;
5526e819
VS
137 return m_Pos;
138 }
03c0fc66
VS
139 toskip = nextpos;
140 }
141
142 if ( toskip > 0 )
143 {
144 const size_t BUFSIZE = 4096;
145 size_t sz;
146 char buffer[BUFSIZE];
147 while ( toskip > 0 )
148 {
149 sz = wxMin(toskip, BUFSIZE);
150 unzReadCurrentFile((unzFile)m_Archive, buffer, sz);
151 toskip -= sz;
152 }
5526e819
VS
153 }
154
155 m_Pos = nextpos;
156 return m_Pos;
157}
158
d78b3d64 159#endif
e90c1d2a 160 // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB