]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | #ifdef __GNUG__ | |
d1af991f | 10 | #pragma implementation "zipstream.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 | ||
d1af991f | 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" | |
26 | #include "wx/zipstream.h" | |
ea4f5235 JS |
27 | |
28 | /* Not the right solution (paths in makefiles) but... */ | |
29 | #ifdef __BORLANDC__ | |
30 | #include "../common/unzip.h" | |
31 | #else | |
5526e819 | 32 | #include "unzip.h" |
ea4f5235 JS |
33 | #endif |
34 | ||
5526e819 | 35 | |
5526e819 VS |
36 | wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file) : wxInputStream() |
37 | { | |
38 | unz_file_info zinfo; | |
39 | ||
40 | m_Pos = 0; | |
41 | m_Size = 0; | |
fc8525d7 | 42 | m_Archive = (void*) unzOpen(archive.mb_str(wxConvFile)); |
d1af991f RR |
43 | if (m_Archive == NULL) |
44 | { | |
5526e819 VS |
45 | m_lasterror = wxStream_READ_ERR; |
46 | return; | |
47 | } | |
fc8525d7 | 48 | if (unzLocateFile((unzFile)m_Archive, file.mb_str(wxConvFile), 0) != UNZ_OK) |
d1af991f | 49 | { |
5526e819 VS |
50 | m_lasterror = wxStream_READ_ERR; |
51 | return; | |
52 | } | |
d1af991f RR |
53 | |
54 | unzGetCurrentFileInfo((unzFile)m_Archive, &zinfo, (char*) NULL, 0, (void*) NULL, 0, (char*) NULL, 0); | |
5526e819 | 55 | |
d1af991f RR |
56 | if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) |
57 | { | |
5526e819 VS |
58 | m_lasterror = wxStream_READ_ERR; |
59 | return; | |
60 | } | |
61 | m_Size = zinfo.uncompressed_size; | |
62 | } | |
63 | ||
64 | ||
65 | ||
66 | wxZipInputStream::~wxZipInputStream() | |
67 | { | |
d1af991f RR |
68 | if (m_Archive) |
69 | { | |
5526e819 VS |
70 | if (m_Size != 0) |
71 | unzCloseCurrentFile((unzFile)m_Archive); | |
72 | unzClose((unzFile)m_Archive); | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | ||
78 | size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize) | |
79 | { | |
80 | if (m_Pos + bufsize > m_Size) bufsize = m_Size - m_Pos; | |
81 | unzReadCurrentFile((unzFile)m_Archive, buffer, bufsize); | |
82 | m_Pos += bufsize; | |
83 | return bufsize; | |
84 | } | |
85 | ||
86 | ||
87 | ||
88 | off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode) | |
89 | { | |
90 | off_t nextpos; | |
91 | void *buf; | |
92 | ||
d1af991f RR |
93 | switch (mode) |
94 | { | |
5526e819 VS |
95 | case wxFromCurrent : nextpos = seek + m_Pos; break; |
96 | case wxFromStart : nextpos = seek; break; | |
97 | case wxFromEnd : nextpos = m_Size - 1 + seek; break; | |
98 | default : nextpos = m_Pos; break; /* just to fool compiler, never happens */ | |
99 | } | |
100 | ||
101 | // cheated seeking : | |
d1af991f RR |
102 | if (nextpos > m_Pos) |
103 | { | |
5526e819 VS |
104 | buf = malloc(nextpos - m_Pos); |
105 | unzReadCurrentFile((unzFile)m_Archive, buf, nextpos - m_Pos); | |
106 | free(buf); | |
107 | } | |
108 | else if (nextpos < m_Pos) { | |
109 | unzCloseCurrentFile((unzFile)m_Archive); | |
d1af991f RR |
110 | if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) |
111 | { | |
5526e819 VS |
112 | m_lasterror = wxStream_READ_ERR; |
113 | return m_Pos; | |
114 | } | |
115 | buf = malloc(nextpos); | |
116 | unzReadCurrentFile((unzFile)m_Archive, buf, nextpos); | |
117 | free(buf); | |
118 | } | |
119 | ||
120 | m_Pos = nextpos; | |
121 | return m_Pos; | |
122 | } | |
123 | ||
d78b3d64 | 124 | #endif |
d1af991f | 125 | // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB |