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