【发布时间】:2015-12-07 23:04:17
【问题描述】:
我想制作一些用于压缩文件的 Android Studio 应用。这是我的代码块:
String input=Environment.getExternalStorageDirectory() +File.separator + "merhaba";
String output = Environment.getExternalStorageDirectory() + File.separator +"TollCulator";
zipFolder(input,output);
这是我的功能:
private static void zipFolder(String inputFolderPath, String outZipPath) {
try {
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Log.d("", "Zip directory: " + srcFile.getName());
for (int i = 0; i < files.length; i++) {
Log.d("", "Adding file: " + files[i].getName());
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
}
我也在我的 XML 文件中使用它:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
那不适合我。我的应用程序没有任何错误。没有任何操作,我的内部存储中也没有 zip 文件。
【问题讨论】:
-
感谢您的帮助,但我只是想知道为什么我的代码块不起作用?
-
我认为我的功能是正确的,因为我从 stackoverflow 中获取了它。但是我的带有路径的代码块可能是错误的。你能帮忙吗?
标签: android file zip fileoutputstream zipoutputstream