OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.mojo_shell_apk; | 5 package org.chromium.mojo_shell_apk; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 | 8 |
9 import java.io.BufferedInputStream; | 9 import java.io.BufferedInputStream; |
10 import java.io.BufferedOutputStream; | 10 import java.io.BufferedOutputStream; |
11 import java.io.File; | 11 import java.io.File; |
12 import java.io.FileInputStream; | 12 import java.io.FileInputStream; |
13 import java.io.FileNotFoundException; | 13 import java.io.FileNotFoundException; |
14 import java.io.FileOutputStream; | 14 import java.io.FileOutputStream; |
15 import java.io.IOException; | 15 import java.io.IOException; |
16 import java.io.InputStream; | 16 import java.io.InputStream; |
17 import java.io.OutputStream; | 17 import java.io.OutputStream; |
18 import java.util.zip.ZipEntry; | 18 import java.util.zip.ZipEntry; |
19 import java.util.zip.ZipInputStream; | 19 import java.util.zip.ZipInputStream; |
20 | 20 |
21 /** | 21 /** |
22 * Helper methods for file extraction from APK assets and zip archives. | 22 * Helper methods for file extraction from APK assets and zip archives. |
23 */ | 23 */ |
24 class FileHelper { | 24 class FileHelper { |
25 // Size of the buffer used in streaming file operations. | 25 // Size of the buffer used in streaming file operations. |
26 private static final int BUFFER_SIZE = 1024 * 1024; | 26 private static final int BUFFER_SIZE = 1024 * 1024; |
27 // Prefix used when naming temporary files. | 27 // Prefix used when naming temporary files. |
28 private static final String TEMP_FILE_PREFIX = "temp-"; | 28 private static final String TEMP_FILE_PREFIX = "temp-"; |
29 | 29 |
30 static File extractFromAssets(Context context, String assetName, File output
Directory) | 30 static File extractFromAssets(Context context, String assetName, File output
Directory, |
31 throws IOException, FileNotFoundException { | 31 boolean useTempFile) throws IOException, FileNotFoundException { |
32 // Make the original filename part of the temp file name. | 32 File outputFile; |
33 // TODO(ppi): do we need to sanitize the suffix? | 33 if (useTempFile) { |
34 String suffix = "-" + assetName; | 34 // Make the original filename part of the temp file name. |
35 File outputFile = File.createTempFile(TEMP_FILE_PREFIX, suffix, outputDi
rectory); | 35 // TODO(ppi): do we need to sanitize the suffix? |
| 36 String suffix = "-" + assetName; |
| 37 outputFile = File.createTempFile(TEMP_FILE_PREFIX, suffix, outputDir
ectory); |
| 38 } else { |
| 39 outputFile = new File(outputDirectory, assetName); |
| 40 } |
| 41 |
36 BufferedInputStream inputStream = new BufferedInputStream( | 42 BufferedInputStream inputStream = new BufferedInputStream( |
37 context.getAssets().open(assetName)); | 43 context.getAssets().open(assetName)); |
38 writeStreamToFile(inputStream, outputFile); | 44 writeStreamToFile(inputStream, outputFile); |
39 inputStream.close(); | 45 inputStream.close(); |
40 return outputFile; | 46 return outputFile; |
41 } | 47 } |
42 | 48 |
43 /** | 49 /** |
44 * Extracts the file of the given extension from the archive. Throws FileNot
FoundException if no | 50 * Extracts the file of the given extension from the archive. Throws FileNot
FoundException if no |
45 * matching file is found. | 51 * matching file is found. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 throws IOException { | 87 throws IOException { |
82 byte[] buffer = new byte[BUFFER_SIZE]; | 88 byte[] buffer = new byte[BUFFER_SIZE]; |
83 OutputStream outputStream = new BufferedOutputStream(new FileOutputStrea
m(outputFile)); | 89 OutputStream outputStream = new BufferedOutputStream(new FileOutputStrea
m(outputFile)); |
84 int read; | 90 int read; |
85 while ((read = inputStream.read(buffer, 0, BUFFER_SIZE)) > 0) { | 91 while ((read = inputStream.read(buffer, 0, BUFFER_SIZE)) > 0) { |
86 outputStream.write(buffer, 0, read); | 92 outputStream.write(buffer, 0, read); |
87 } | 93 } |
88 outputStream.close(); | 94 outputStream.close(); |
89 } | 95 } |
90 } | 96 } |
OLD | NEW |