【问题标题】:Android unzipping files that where zipped on a MacAndroid解压缩在Mac上压缩的文件
【发布时间】: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 file (zipped on a Mac)

Zip file (zippen on Win7)

Demo.apk

【问题讨论】:

  • 您的虚拟主机已暂停下载示例 zip 文件。
  • 谢谢!我已经更新了示例文件。

标签: android macos windows-7 osx-lion zipfile


【解决方案1】:

这些文件非常不同...很难找到重要的差异。

【讨论】:

  • true,Mac 会自动添加 __MACOSX 目录,或者类似的东西。我喜欢用 hexeditor 分析 zip 文件的想法。现在,我知道有区别,但我想知道如何能够在我的 Android 设备上提取 mac-zipped-file。因为 size=-1 导致程序冻结
  • 我已经更新了 win_zip 文件。它现在应该是一个精确的副本,例如包括 __MACOSX 目录。
【解决方案2】:

问题与版本有关。让我从我的 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 应用程序可以膨胀的输出。

【讨论】:

  • @Devunwired,我正在尝试解压缩文件,但我仍然可以在 Android 中解压缩 1.0 fat,问题是我无法为 Android 解压缩 2.0 fat。是否有任何代码或库可以为所有人解压缩Android 中的 zip 文件类型
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2013-03-09
  • 1970-01-01
  • 2010-09-05
  • 2011-12-03
  • 2013-10-30
相关资源
最近更新 更多