我想从服务器下载zip文件并以编程方式从sdcard中提取,但它不会提取zip文件。
我查看了本教程
http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29
我使用下面的代码,但它对我不起作用
UnZipActivity.java
public class UnZipActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UnZip un = new UnZip("/storage/emulated/0/peakmedia/news.zip","/storage/emulated/0/peakmedia/");
un.unzip();
}
}
UnZip.java
public class UnZip {
public String _zipFile;
public String _location;
public UnZip(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
Logcat错误:
02-03 15:23:09.125: E/Decompress(4936): unzip
02-03 15:23:09.125: E/Decompress(4936): java.io.FileNotFoundException: /storage/emulated/0/peakmedia/news/jquery.backstretch.min.js: open failed: ENOENT (No such file or directory)
02-03 15:23:09.125: E/Decompress(4936): at libcore.io.IoBridge.open(IoBridge.java:416)
02-03 15:23:09.125: E/Decompress(4936): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
02-03 15:23:09.125: E/Decompress(4936): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
02-03 15:23:09.125: E/Decompress(4936): at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
02-03 15:23:09.125: E/Decompress(4936): at com.peak.media.UnZip.unzip(UnZip.java:49)
02-03 15:23:09.125: E/Decompress(4936): at com.peak.media.UnZipActivity.onCreate(UnZipActivity.java:14)
02-03 15:23:09.125: E/Decompress(4936): at android.app.Activity.performCreate(Activity.java:5326)
02-03 15:23:09.125: E/Decompress(4936): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-03 15:23:09.125: E/Decompress(4936): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
02-03 15:23:09.125: E/Decompress(4936): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
02-03 15:23:09.125: E/Decompress(4936): at android.app.ActivityThread.access$700(ActivityThread.java:158)
02-03 15:23:09.125: E/Decompress(4936): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
02-03 15:23:09.125: E/Decompress(4936): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 15:23:09.125: E/Decompress(4936): at android.os.Looper.loop(Looper.java:176)
02-03 15:23:09.125: E/Decompress(4936): at android.app.ActivityThread.main(ActivityThread.java:5365)
02-03 15:23:09.125: E/Decompress(4936): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 15:23:09.125: E/Decompress(4936): at java.lang.reflect.Method.invoke(Method.java:511)
02-03 15:23:09.125: E/Decompress(4936): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-03 15:23:09.125: E/Decompress(4936): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-03 15:23:09.125: E/Decompress(4936): at dalvik.system.NativeStart.main(Native Method)
02-03 15:23:09.125: E/Decompress(4936): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-03 15:23:09.125: E/Decompress(4936): at libcore.io.Posix.open(Native Method)
02-03 15:23:09.125: E/Decompress(4936): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-03 15:23:09.125: E/Decompress(4936): at libcore.io.IoBridge.open(IoBridge.java:400)
02-03 15:23:09.125: E/Decompress(4936): ... 19 more