Skip to content

Commit 8ec8343

Browse files
committed
[android] Fix java runner to not assume zip format when unzipping
After dotnet#112256 landed, there was a change to zip the test assets using `ZipFile.CreateFromDirectory` for cross platform support. This regressed `unzipAssets` in `MonoRunner.java` because it naivley assumed that directories would come before files in the zip archive. This change fixes the problem by making sure directories are created first before writing files to disk. Fixes dotnet#112558
1 parent 6e06aef commit 8ec8343

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/tasks/AndroidAppBuilder/Templates/MonoRunner.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,33 +146,33 @@ public void onDestroy() {
146146

147147
static void unzipAssets(Context context, String toPath, String zipName) {
148148
AssetManager assetManager = context.getAssets();
149-
try {
150-
InputStream inputStream = assetManager.open(zipName);
151-
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
149+
try (InputStream inputStream = assetManager.open(zipName);
150+
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream))) {
151+
152152
ZipEntry zipEntry;
153153
byte[] buffer = new byte[4096];
154154
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
155155
String fileOrDirectory = zipEntry.getName();
156-
Uri.Builder builder = new Uri.Builder();
157-
builder.scheme("file");
158-
builder.appendPath(toPath);
159-
builder.appendPath(fileOrDirectory);
160-
String fullToPath = builder.build().getPath();
161-
if (zipEntry.isDirectory()) {
162-
File directory = new File(fullToPath);
163-
directory.mkdirs();
164-
continue;
156+
File file = new File(toPath, fileOrDirectory);
157+
File parent = new File(file.getParent());
158+
159+
if (file.isDirectory()) {
160+
file.mkdirs();
161+
}
162+
else if (!parent.exists()) {
163+
parent.mkdirs();
165164
}
165+
166+
String fullToPath = file.getAbsolutePath();
166167
Log.i("DOTNET", "Extracting asset to " + fullToPath);
168+
167169
int count = 0;
168170
FileOutputStream fileOutputStream = new FileOutputStream(fullToPath);
169171
while ((count = zipInputStream.read(buffer)) != -1) {
170172
fileOutputStream.write(buffer, 0, count);
171173
}
172-
fileOutputStream.close();
173174
zipInputStream.closeEntry();
174175
}
175-
zipInputStream.close();
176176
} catch (IOException e) {
177177
Log.e("DOTNET", e.getLocalizedMessage());
178178
}

0 commit comments

Comments
 (0)