Android 解压缩在 Mac 上压缩的文件

标签 android macos windows-7 osx-lion zip

我有一个应用程序,它下载一个 zip 文件并将文件解压缩到我的 SDCard 上。 一切正常,但是当我的同事在他的 Mac (lion) 上创建 zip 文件时,我的所有文件都有

尺寸:-1

crc:-1

压缩后大小:-1

我无法将文件写入我的 SD 卡。两个 zip 的内容完全相同,唯一的区别是它们最初压缩的位置。这是我解压缩文件的代码:

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 file (zipped on a Mac)

Zip file (zippen on Win7)

Demo.apk

最佳答案

问题与版本有关。让我从我的 Mac (10.8) 的一些输出开始:

~ $ zipinfo -m test_mac.zip 
Archive: test_mac.zip   1694 bytes   8 files
drwxr-xr-x  2.1 unx        0 bx  0% stor 10-Aug-12 01:11 test_win/
-rwxr-xr-x  2.1 unx       46 bX 20% defN 10-Aug-12 01:11 test_win/index.html
drwxrwxr-x  2.1 unx        0 bx  0% stor 10-Aug-12 01:12 __MACOSX/
drwxrwxr-x  2.1 unx        0 bx  0% stor 10-Aug-12 01:12 __MACOSX/test_win/
-rw-r--r--  2.1 unx      211 bX 37% defN 10-Aug-12 01:11 __MACOSX/test_win/._index.html
-rwxr-xr-x  2.1 unx        9 bX-21% defN 10-Aug-12 01:10 test_win/version.txt
-rw-r--r--  2.1 unx      211 bX 37% defN 10-Aug-12 01:10 __MACOSX/test_win/._version.txt
-rw-r--r--  2.1 unx      211 bX 37% defN 10-Aug-12 01:11 __MACOSX/._test_win
8 files, 688 bytes uncompressed, 450 bytes compressed:  34.6%
~ $ zipinfo -m test_win.zip 
Archive: test_win.zip   1678 bytes   8 files
drwx---     3.1 fat        0 bx  0% stor 10-Aug-12 09:11 test_win/
-rw-a--     3.1 fat       46 bx 20% defN 10-Aug-12 09:11 test_win/index.html
-rw-a--     3.1 fat        9 bx-21% defN 10-Aug-12 09:10 test_win/version.txt
drwx---     3.1 fat        0 bx  0% stor 10-Aug-12 09:12 __MACOSX/
-rw-a--     3.1 fat      211 bx 37% defN 10-Aug-12 09:11 __MACOSX/._test_win
drwx---     3.1 fat        0 bx  0% stor 10-Aug-12 09:12 __MACOSX/test_win/
-rw-a--     3.1 fat      211 bx 37% defN 10-Aug-12 09:11 __MACOSX/test_win/._index.html
-rw-a--     3.1 fat      211 bx 37% defN 10-Aug-12 09:10 __MACOSX/test_win/._version.txt
8 files, 688 bytes uncompressed, 450 bytes compressed:  34.6%

查看第二个字段(mac 文件中的 2.1 和 win 文件中的 3.1)。这是压缩文件所依据的 ZIP 存档格式版本。 java.util.zip 实现仅支持 2.50 版及更高版本的 ZIP 文件格式(参见 StackOverflow)。

Mac 的 Compress ... 菜单选项使用的版本低于 Java 实现支持的版本(2.1 仍在 10.8 中使用)。

告诉您的同事改用命令行工具(例如 zip -r myfile.zip directory_to_compress/),您应该会得到 Android 应用程序可以扩充的输出。

关于Android 解压缩在 Mac 上压缩的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11902943/

相关文章:

android - IONIC 字体大小在不同设备上看起来不同

java - 对象到 fragment 通信

android - 在Android 4.2上调整Mediacodec解码器的缓冲区大小

windows-7 - webproxy.exe 进程导致 CPU 使用率 100%

android - 在特定时间推送通知

macos - NSOpenPanel - Cmd+A 快捷键不起作用

windows - 为什么从函数返回的 gstrings 串联是一个空字符串 - Groovy 1.7.4

mysql - 升级到 Mountain Lion 后无法访问 MySQL 数据库(使用 MAMP)

windows-7 - Windows 7 : AppData folder not visible in windows explorer

c# - 如何将 'Pin this program to the taskbar' 添加到 Windows 7 的跳转列表中?