-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfile.cpp
121 lines (103 loc) · 2.15 KB
/
zfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "zfile.h"
#include "zipfs.h"
#include "zlib.h"
#include "zutil.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/stat.h>
#include "zipfile.h"
ZFile::ZFile()
{
m_fd = NULL;
m_fe = NULL;
}
ZFile::~ZFile()
{
af_close(m_fd);
m_fe->getRoot()->release();
}
ZFile *
ZFile::open(const char *file_name, const char *sub_name)
{
ZRootEntry *root = ZDirEntry::getEntry(file_name);
if( root==NULL ) return NULL;
ZEntry *entry = (ZFileEntry*)(root->findByName(sub_name));
if( entry==NULL ) return NULL;
if( entry->isDir() ) return NULL;
ZFileEntry *f_entry = (ZFileEntry*)entry;
AF_FILE fd;
fd = zip_open(root->m_fd,
f_entry->m_pos, f_entry->m_csize, f_entry->m_size);
if (fd==NULL) return NULL;
ZFile *zf = new ZFile();
if (NULL==zf) {
af_close(fd);
return NULL;
}
zf->m_fe = f_entry;
zf->m_fd = fd;
root->grab();
return zf;
}
bool
ZFile::isDir(const char *file_name, const char *sub_name)
{
ZRootEntry *root = ZDirEntry::getEntry(file_name);
if (!root) return false;
ZEntry *entry = root->findByName(sub_name);
if (entry)
return entry->isDir();
else
return false;
}
bool
ZFile::isFile(const char *file_name, const char *sub_name)
{
ZRootEntry *root = ZDirEntry::getEntry(file_name);
if (!root) return false;
ZEntry *entry = root->findByName(sub_name);
if (entry)
return !entry->isDir();
else
return false;
}
int
ZFile::read(char *buffer, int length)
{
return af_read(m_fd, (ZF_UCHAR8*)buffer, length);
}
int
ZFile::seek(int offset, int whence)
{
int new_whence;
switch(whence) {
case SEEK_SET:
new_whence = ZF_SEEK_SET;
break;
case SEEK_CUR:
new_whence = ZF_SEEK_CUR;
break;
case SEEK_END:
new_whence = ZF_SEEK_END;
break;
default:
return -1;
}
return af_seek(m_fd, offset, new_whence);
}
int
ZFile::tell()
{
return af_seek(m_fd, 0, ZF_SEEK_CUR);
}
int
ZFile::size()
{
return af_size(m_fd);
}
void
ZFileSystemGC()
{
ZDirEntry::GC();
}