2012-08-10 110 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