]>
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" | |
5526e819 VS |
27 | #include "unzip.h" |
28 | ||
5526e819 VS |
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; | |
fc8525d7 | 35 | m_Archive = (void*) unzOpen(archive.mb_str(wxConvFile)); |
d1af991f RR |
36 | if (m_Archive == NULL) |
37 | { | |
5526e819 VS |
38 | m_lasterror = wxStream_READ_ERR; |
39 | return; | |
40 | } | |
fc8525d7 | 41 | if (unzLocateFile((unzFile)m_Archive, file.mb_str(wxConvFile), 0) != UNZ_OK) |
d1af991f | 42 | { |
5526e819 VS |
43 | m_lasterror = wxStream_READ_ERR; |
44 | return; | |
45 | } | |
d1af991f RR |
46 | |
47 | unzGetCurrentFileInfo((unzFile)m_Archive, &zinfo, (char*) NULL, 0, (void*) NULL, 0, (char*) NULL, 0); | |
5526e819 | 48 | |
d1af991f RR |
49 | if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) |
50 | { | |
5526e819 VS |
51 | m_lasterror = wxStream_READ_ERR; |
52 | return; | |
53 | } | |
54 | m_Size = zinfo.uncompressed_size; | |
55 | } | |
56 | ||
57 | ||
58 | ||
59 | wxZipInputStream::~wxZipInputStream() | |
60 | { | |
d1af991f RR |
61 | if (m_Archive) |
62 | { | |
5526e819 VS |
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 | ||
d1af991f RR |
86 | switch (mode) |
87 | { | |
5526e819 VS |
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 : | |
d1af991f RR |
95 | if (nextpos > m_Pos) |
96 | { | |
5526e819 VS |
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); | |
d1af991f RR |
103 | if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) |
104 | { | |
5526e819 VS |
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 | ||
d78b3d64 | 117 | #endif |
d1af991f | 118 | // wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB |