]> git.saurik.com Git - wxWidgets.git/blob - src/common/zipstream.cpp
Some minors changes, like adding empty lines at end of files, to be able to compile...
[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 #include <wx/stream.h>
24 #include <wx/wfstream.h>
25 #include <wx/zipstream.h>
26 #include "unzip.h"
27
28
29
30
31 wxZipInputStream::wxZipInputStream(const wxString& archive, const wxString& file) : wxInputStream()
32 {
33 unz_file_info zinfo;
34
35 m_Pos = 0;
36 m_Size = 0;
37 m_Archive = (void*) unzOpen(archive);
38 if (m_Archive == NULL) {
39 m_lasterror = wxStream_READ_ERR;
40 return;
41 }
42 if (unzLocateFile((unzFile)m_Archive, file, 0) != UNZ_OK) {
43 m_lasterror = wxStream_READ_ERR;
44 return;
45 }
46 unzGetCurrentFileInfo((unzFile)m_Archive, &zinfo, NULL, 0, NULL, 0, NULL, 0);
47
48 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) {
49 m_lasterror = wxStream_READ_ERR;
50 return;
51 }
52 m_Size = zinfo.uncompressed_size;
53 }
54
55
56
57 wxZipInputStream::~wxZipInputStream()
58 {
59 if (m_Archive) {
60 if (m_Size != 0)
61 unzCloseCurrentFile((unzFile)m_Archive);
62 unzClose((unzFile)m_Archive);
63 }
64 }
65
66
67
68 size_t wxZipInputStream::OnSysRead(void *buffer, size_t bufsize)
69 {
70 if (m_Pos + bufsize > m_Size) bufsize = m_Size - m_Pos;
71 unzReadCurrentFile((unzFile)m_Archive, buffer, bufsize);
72 m_Pos += bufsize;
73 return bufsize;
74 }
75
76
77
78 off_t wxZipInputStream::OnSysSeek(off_t seek, wxSeekMode mode)
79 {
80 off_t nextpos;
81 void *buf;
82
83 switch (mode) {
84 case wxFromCurrent : nextpos = seek + m_Pos; break;
85 case wxFromStart : nextpos = seek; break;
86 case wxFromEnd : nextpos = m_Size - 1 + seek; break;
87 default : nextpos = m_Pos; break; /* just to fool compiler, never happens */
88 }
89
90 // cheated seeking :
91 if (nextpos > m_Pos) {
92 buf = malloc(nextpos - m_Pos);
93 unzReadCurrentFile((unzFile)m_Archive, buf, nextpos - m_Pos);
94 free(buf);
95 }
96 else if (nextpos < m_Pos) {
97 unzCloseCurrentFile((unzFile)m_Archive);
98 if (unzOpenCurrentFile((unzFile)m_Archive) != UNZ_OK) {
99 m_lasterror = wxStream_READ_ERR;
100 return m_Pos;
101 }
102 buf = malloc(nextpos);
103 unzReadCurrentFile((unzFile)m_Archive, buf, nextpos);
104 free(buf);
105 }
106
107 m_Pos = nextpos;
108 return m_Pos;
109 }
110