我有一个代码在模拟器上运行得非常好,但是当我在三星 Galaxy Tab 上运行它时,它给出了异常。
我通过套接字从服务器接收一个压缩的 zip 文件,然后提取这些文件。如果我压缩并发送两个或三个文本文件,它可以在模拟器和 Galaxy Tab 上运行良好。
但是,如果我压缩并发送一些带有文本或两个图像文件的小图像文件,它会给出:>java.util.zip.ZipException: Central Directory Entry not found < on Galaxy Tab 但在模拟器上没有错误。Zip 文件大小不超过 32 KB,我确信该文件被正确接收。这是我的解压缩代码
package com.vsi.vremote;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.content.Context;
import android.util.Log;
public class UnCompressor {
private static final String TAG = "UnCompressor";
Context context;
public UnCompressor(Context context) {
this.context = context;
}
private final void copyInputStream(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public final String[] unCompress(String name) {
try {
Log.d(TAG, "Uncompress called");
ZipFile zipFile = new ZipFile(context.getFileStreamPath(name));
Log.d(TAG, "Zip file created");
Enumeration entries = zipFile.entries();
String fileNames[] = new String[zipFile.size()];
int counter = 0;
Log.d(TAG, "Loop strting");
while (entries.hasMoreElements()) {
Log.d(TAG, "Getting next entry");
ZipEntry entry = (ZipEntry) entries.nextElement();
Log.d(TAG, "Extracting file: " + entry.getName());
copyInputStream(
zipFile.getInputStream(entry),
new BufferedOutputStream(context.openFileOutput(
entry.getName(), Context.MODE_PRIVATE)));
fileNames[counter++] = entry.getName();
}
zipFile.close();
return fileNames;
} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return null;
}
}
public final void delete(String fileName) {
context.deleteFile(fileName);
}
}
注意:我刚刚在我的 HTC WildFire 上检查过,它也在这个手机上工作,但 Galaxy TAB :(