【发布时间】:2012-08-07 19:56:57
【问题描述】:
我有一个应用程序,它下载一个 zip 文件并解压缩我 SDCard 上的文件。 一切正常,但是当我的同事在他的 Mac(狮子)上创建 zip 文件时,我的所有文件都有
尺寸:-1
crc:-1
压缩尺寸:-1
我无法将文件写入我的 SD 卡。两个拉链具有完全相同的内容,唯一的区别是它们最初压缩的位置。这是我解压缩文件的代码:
public class UnzipTask extends AsyncTask<String, Integer, Void> {
private static final String TAG = UnzipTask.class.getSimpleName();
private String mDestLocation;
private ZipListener mListener;
private Context mCtx;
private int mCallbackId;
public UnzipTask(Context context, ZipListener listener, File dir)
{
mCtx = context;
mListener = listener;
mDestLocation = dir.getAbsolutePath() + "/";
}
public void setId(int id)
{
mCallbackId = id;
}
@Override
protected Void doInBackground(String... arg0) {
try {
String file = arg0[0];
InputStream is = mCtx.getAssets().open(file);
unzipFile(is);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Private function that ensures a directory exist
* @param dir
*/
private void _dirChecker(String dir) {
File f = new File(mDestLocation + dir);
if (!f.isDirectory()) {
f.mkdirs();
}
}
private void unzipFile(InputStream input) throws Exception {
ZipInputStream zin = new ZipInputStream(input);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v(TAG, "Unzipping " + ze.getName());
if(mListener != null)
{
mListener.onUnzipped(mCallbackId, ze.getName(), ze.g etSize(), ze.getCrc(), ze.getCompressedSize());
}
if (ze.isDirectory()) {
_dirChecker(ze.getName());
} else if (ze.getCompressedSize() > 0 && ze.getSize() > 0 && ze.getCrc() != 0.0) {
// If size=-1 -> writing to disk fails
String fileOutput = mDestLocation + ze.getName();
FileOutputStream fout = new FileOutputStream(fileOutput);
int read = 0;
byte[] buffer = new byte[(int) ze.getSize()];
while ((read = zin.read(buffer)) >= 0) {
fout.write(buffer, 0, read);
}
zin.closeEntry();
fout.close();
} else {
Log.v(TAG, "Skipping entry" + ze.getName());
}
}
}
zin.close();
}
}
几点说明
1)我可以在我的 Windows 7 电脑上解压缩这两个文件
2)我的同事可以在他的 Mac 上解压缩这两个文件
3) 唯一的问题是,在 Android 上我无法解压缩 MAC 创建的 zip 文件...
问题:
有谁知道为什么在 Mac 上压缩的 zip 文件有这些无效的大小?我的解压过程(在 Android 上)是否缺少一些代码?
如果需要,您可以在此处下载 zip,以及用于显示输出的非常小的 apk:
编辑:更新链接
【问题讨论】:
-
您的虚拟主机已暂停下载示例 zip 文件。
-
谢谢!我已经更新了示例文件。
标签: android macos windows-7 osx-lion zipfile