/
AndroidZipFileHandle.java
196 lines (173 loc) · 5.99 KB
/
AndroidZipFileHandle.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.backends.android;
import java.io.*;
import android.content.res.AssetFileDescriptor;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.backends.android.ZipResourceFile.ZipEntryRO;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;
/** @author sarkanyi */
public class AndroidZipFileHandle extends AndroidFileHandle {
private boolean hasAssetFd;
private long fdLength;
private ZipResourceFile expansionFile;
private String path;
public AndroidZipFileHandle (String fileName) {
super(null, fileName, FileType.Internal);
initialize();
}
public AndroidZipFileHandle (File file, FileType type) {
super(null, file, type);
initialize();
}
private void initialize () {
path = file.getPath().replace('\\', '/');
expansionFile = ((AndroidFiles)Gdx.files).getExpansionFile();
AssetFileDescriptor assetFd = expansionFile.getAssetFileDescriptor(getPath());
if (assetFd != null) {
hasAssetFd = true;
fdLength = assetFd.getLength();
try {
assetFd.close();
} catch (IOException e) {
}
} else {
hasAssetFd = false;
}
// needed for listing entries and exists() of directories
if (isDirectory()) path += "/";
}
@Override
public AssetFileDescriptor getAssetFileDescriptor () throws IOException {
return expansionFile.getAssetFileDescriptor(getPath());
}
private String getPath () {
return path;
}
@Override
public InputStream read () {
InputStream input = null;
try {
input = expansionFile.getInputStream(getPath());
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading file: " + file + " (ZipResourceFile)", ex);
}
return input;
}
@Override
public FileHandle child (String name) {
if (file.getPath().length() == 0) return new AndroidZipFileHandle(new File(name), type);
return new AndroidZipFileHandle(new File(file, name), type);
}
@Override
public FileHandle sibling (String name) {
if (file.getPath().length() == 0) throw new GdxRuntimeException("Cannot get the sibling of the root.");
return Gdx.files.getFileHandle(new File(file.getParent(), name).getPath(), type); // this way we can find the sibling even
// if it's not inside the obb
}
@Override
public FileHandle parent () {
File parent = file.getParentFile();
if (parent == null) parent = new File("");
return new AndroidZipFileHandle(parent.getPath());
}
@Override
public FileHandle[] list () {
ZipEntryRO[] zipEntries = expansionFile.getEntriesAt(getPath());
FileHandle[] handles = new FileHandle[zipEntries.length - 1];
int count = 0;
for (int i = 0, n = zipEntries.length; i < n; i++) {
if (zipEntries[i].mFileName.length() == getPath().length()) // Don't include the directory itself
continue;
handles[count++] = new AndroidZipFileHandle(zipEntries[i].mFileName);
}
return handles;
}
@Override
public FileHandle[] list (FileFilter filter) {
ZipEntryRO[] zipEntries = expansionFile.getEntriesAt(getPath());
FileHandle[] handles = new FileHandle[zipEntries.length - 1];
int count = 0;
for (int i = 0, n = zipEntries.length; i < n; i++) {
if (zipEntries[i].mFileName.length() == getPath().length()) // Don't include the directory itself
continue;
FileHandle child = new AndroidZipFileHandle(zipEntries[i].mFileName);
if (!filter.accept(child.file())) continue;
handles[count] = child;
count++;
}
if (count < handles.length) {
FileHandle[] newHandles = new FileHandle[count];
System.arraycopy(handles, 0, newHandles, 0, count);
handles = newHandles;
}
return handles;
}
@Override
public FileHandle[] list (FilenameFilter filter) {
ZipEntryRO[] zipEntries = expansionFile.getEntriesAt(getPath());
FileHandle[] handles = new FileHandle[zipEntries.length - 1];
int count = 0;
for (int i = 0, n = zipEntries.length; i < n; i++) {
if (zipEntries[i].mFileName.length() == getPath().length()) // Don't include the directory itself
continue;
String path = zipEntries[i].mFileName;
if (!filter.accept(file, path)) continue;
handles[count] = new AndroidZipFileHandle(path);
count++;
}
if (count < handles.length) {
FileHandle[] newHandles = new FileHandle[count];
System.arraycopy(handles, 0, newHandles, 0, count);
handles = newHandles;
}
return handles;
}
@Override
public FileHandle[] list (String suffix) {
ZipEntryRO[] zipEntries = expansionFile.getEntriesAt(getPath());
FileHandle[] handles = new FileHandle[zipEntries.length - 1];
int count = 0;
for (int i = 0, n = zipEntries.length; i < n; i++) {
if (zipEntries[i].mFileName.length() == getPath().length()) // Don't include the directory itself
continue;
String path = zipEntries[i].mFileName;
if (!path.endsWith(suffix)) continue;
handles[count] = new AndroidZipFileHandle(path);
count++;
}
if (count < handles.length) {
FileHandle[] newHandles = new FileHandle[count];
System.arraycopy(handles, 0, newHandles, 0, count);
handles = newHandles;
}
return handles;
}
@Override
public boolean isDirectory () {
return !hasAssetFd;
}
@Override
public long length () {
return hasAssetFd ? fdLength : 0;
}
@Override
public boolean exists () {
return hasAssetFd || expansionFile.getEntriesAt(getPath()).length != 0;
}
}