2012-08-10 104 views
3

我有一个应用程序,用于下载zip文件并解压缩我的SD卡上的文件。 一切工作正常,但是当我collegue建立在他的Mac(狮子)的zip文件,我的所有文件都Android解压缩在Mac上压缩的文件

尺寸:-1

CRC:-1

compressedsize :-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)唯一的问题是o位于Android我不能解压缩zip文件中的MAC创建...

问题:

有谁知道为什么这是在Mac上拉链的zip文件具有这些无效的大小?我的解压缩过程(在Android上)是否缺少一些代码?

您可以在这里下载拉链,如果你想,还有一个非常小的apk显示输出:

编辑:更新的链接

Zip file (zipped on a Mac)

Zip file (zippen on Win7)

Demo.apk

+0

您的虚拟主机已暂停下载示例zip文件。 – Barend 2012-08-13 18:13:07

+0

谢谢!我已经更新了示例文件。 – Entreco 2012-08-13 19:51:50

回答

5

该问题与版本有关。让我先从我的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% 

看一看第二场(2.1在Mac文件和3.1在胜利文件)。这是该文件被压缩的ZIP存档格式版本。 java.util.zip实现仅支持ZIP文件格式的2.50及更高版本(请参见此StackOverflow)。

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

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

+0

优秀的答案。 – Barend 2012-10-04 19:28:01

+0

@Devunwired,我正在尝试解压缩文件,但我仍然可以在Android中解压缩1.0脂肪,问题是我无法为Android 2.0脂肪。是否有任何代码或库可以解压缩Android中的所有压缩文件类型 – 2017-03-14 04:59:49

+0

Here是我的两个文件不同.http://imgur.com/a/y9iiO – 2017-03-14 05:00:56

0

这些文件是非常不同的......很难寻找重要的差异CES。 enter image description here

+0

确实如此,Mac会自动添加__MACOSX目录或类似的东西。我喜欢用hexeditor分析zip文件的想法。现在,我知道有一个区别,但我想知道如何能够在我的Android设备上提取mac-zipped-file。对于大小= -1导致程序冻结 – Entreco 2012-08-13 20:02:22

+0

我已更新win_zip文件。它现在应该是一个精确的副本,例如包括__MACOSX目录。 – Entreco 2012-08-13 20:06:34