]> git.saurik.com Git - wxWidgets.git/blob - src/common/zipstream.cpp
cliet data crash fixed
[wxWidgets.git] / src / common / zipstream.cpp
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
9 #ifdef __GNUG__
10 #pragma implementation "zipstream.h"
11 #endif
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
21
22 #include "wx/log.h"
23 #include "wx/intl.h"
24 #include "wx/stream.h"
25 #include "wx/wfstream.h"
26 #include "wx/zipstream.h"
27 #include "unzip.h"
28
29 wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file) : wxInputStream()
30 {
31 unz_file_info zinfo;
32
33 m_Pos = 0;
34 m_Size = 0;
35 m_Archive = (void*) unzOpen(archive.mb_str(wxConvFile));
36 if (m_Archive == NULL)
37 {
38 m_lasterror = wxStream_READ_ERR;
39 return;
40 }
41 if (unzLocateFile((unzFile)m_Archive, file.mb_str(wxConvFile), 0) != UNZ_OK)
42 {
43 m_lasterror = wxStream_READ_ERR;
44 return;
45 }
46
47 unzGetCurrentFileInfo((unzFile)m_Archive, &zinfo, (char*) NULL, 0, (void*) NULL, 0, (char*) NULL, 0);
48
49 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK)
50 {
51 m_lasterror = wxStream_READ_ERR;
52 return;
53 }
54 m_Size = zinfo.uncompressed_size;
55 }
56
57
58
59 wxZipInputStream::~wxZipInputStream()
60 {
61 if (m_Archive)
62 {
63 if (m_Size != 0)
64 unzCloseCurrentFile((unzFile)m_Archive);
65 unzClose((unzFile)m_Archive);
66 }
67 }
68
69
70
71 size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize)
72 {
73 if (m_Pos + bufsize > m_Size) bufsize = m_Size - m_Pos;
74 unzReadCurrentFile((unzFile)m_Archive, buffer, bufsize);
75 m_Pos += bufsize;
76 return bufsize;
77 }
78
79
80
81 off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode)
82 {
83 off_t nextpos;
84 void *buf;
85
86 switch (mode)
87 {
88 case wxFromCurrent : nextpos = seek + m_Pos; break;
89 case wxFromStart : nextpos = seek; break;
90 case wxFromEnd : nextpos = m_Size - 1 + seek; break;
91 default : nextpos = m_Pos; break; /* just to fool compiler, never happens */
92 }
93
94 // cheated seeking :
95 if (nextpos > m_Pos)
96 {
97 buf = malloc(nextpos - m_Pos);
98 unzReadCurrentFile((unzFile)m_Archive, buf, nextpos - m_Pos);
99 free(buf);
100 }
101 else if (nextpos < m_Pos) {
102 unzCloseCurrentFile((unzFile)m_Archive);
103 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK)
104 {
105 m_lasterror = wxStream_READ_ERR;
106 return m_Pos;
107 }
108 buf = malloc(nextpos);
109 unzReadCurrentFile((unzFile)m_Archive, buf, nextpos);
110 free(buf);
111 }
112
113 m_Pos = nextpos;
114 return m_Pos;
115 }
116
117 #endif
118 // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB