2012-08-10 4 views
3

응용 프로그램이 있는데, zip을 다운로드하고 SDCard에서 파일을 압축 해제합니다. 모두 잘 작동하지만 내의 동료는 자신의 맥 (사자)에 zip 파일을 생성 할 때, 내 모든 파일은안드로이드는 Mac에서 압축 된 파일을 압축 해제합니다.

크기가 : -1

CRC : -1

compressedsize을 : -1

그리고 내 sdcard에 파일을 쓸 수 없습니다. 두 zip은 정확히 같은 내용을 가지고 있지만 유일한 차이점은 원래 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(); 
    } 

} 

나는 7 PC 내 창에 두 파일을 압축을 해제 할 수 있습니다

1) 노트의 몇

2) 내 동료가 Mac에서 파일을 모두 압축 해제 할 수 있습니다.

3) 유일한 문제는 다음과 같습니다. o N 안드로이드 나는 MAC (가) 작성하는 zip 파일을 ... 압축을 풀 수 없습니다

질문 :

사람이 Mac에서 압축 된 zip 파일이 잘못된 크기를 가지고 이유를 알고 있나요? 내 Unzipping 프로세스 (안드로이드에서) 일부 코드가 누락 되었습니까?

원하는 경우 출력 보여 매우 작은 APK뿐만 아니라, 여기에 지퍼를 다운로드 할 수 있습니다

편집 : 업데이트 된 링크

Zip file (zipped on a Mac)

Zip file (zippen on Win7)

Demo.apk

+0

웹 호스트에서 샘플 zip 파일 다운로드를 일시 중단했습니다. – Barend

+0

감사! 샘플 파일을 업데이트했습니다. – Entreco

답변

5

이 문제는 버전과 관련이 있습니다. 내 맥 (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 및 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

+0

@Devunwired, 파일 압축을 풀려는 중입니다.하지만 Android에서 1.0 뚱뚱한 부분을 아직 압축 해제 할 수 있습니다. Android에서 2.0 지방을 사용할 수없는 문제가 있습니다. 안드로이드에서 모든 zip 파일 형식의 압축을 풀 수있는 코드 나 라이브러리가 있습니다. –

+0

여기에 있습니다. 내 두 파일이 다릅니다 .http : //imgur.com/a/y9iiO –

0

이 파일들은 매우 다릅니다 ... 중요한 차이를 찾기가 어렵습니다. ces. enter image description here

+0

사실, Mac은 자동으로 __MACOSX 디렉토리 또는 이와 유사한 것을 추가합니다. 나는 hexeditor로 zip 파일을 분석 할 생각을 좋아합니다. 당장은 차이점을 알고 있지만 Android 기기에서 mac-zipped 파일을 추출하는 방법을 알고 싶습니다. 크기 = -1로 인해 프로그램이 멈추는 경우 – Entreco

+0

win_zip 파일을 업데이트했습니다. 이제는 정확한 사본이어야합니다. __MACOSX 디렉토리를 포함하여. – Entreco

관련 문제