【发布时间】:2014-08-31 18:30:36
【问题描述】:
我想将包含图像的 zip 文件从资产复制到内部存储,然后解压缩。 这是我的代码:
protected void copyFromAssetsToInternalStorage(String filename){
AssetManager assetManager = getAssets();
try {
InputStream input = assetManager.open(filename);
OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE);
copyFile(input, output);
} catch (IOException e) {
e.printStackTrace();
}
}
private void unZipFile(String filename){
try {
ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename));
ZipEntry zipEntry;
while((zipEntry = zipInputStream.getNextEntry()) != null){
FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE);
int length;
byte[] buffer = new byte[1024];
while((length = zipInputStream.read(buffer)) > 0){
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.close();
zipInputStream.closeEntry();
}
zipInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
我有这个错误: java.lang.IllegalArgumentException:文件文件名/包含路径分隔符
我该怎么办?
【问题讨论】:
-
你应该做什么:找出导致错误的行(行号在日志中,可能是这样的:
FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE););查看该行的变量值(使用调试器和该行上的断点)并调查为什么有一个带有路径分隔符的变量(即斜杠)。 (我猜这是因为它是一个匹配目录条目而不是文件条目的 ZipEntry 名称)