Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid unzipping files that where zipped on a Mac
    text
    copied!<p>I have an application, which downloads a zip and unzips the files on my SDCard. All works fine, but when my collegue creates the zip file on his Mac (lion), all my files have </p> <p><strong>size:-1</strong> </p> <p><strong>crc:-1</strong> </p> <p><strong>compressedsize:-1</strong></p> <p>and I cannot write the files to my sdcard. Both zips have exactly the same content, the only difference is where they where originally zipped. This is the code where I unzip the files:</p> <pre><code>public class UnzipTask extends AsyncTask&lt;String, Integer, Void&gt; { 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() &gt; 0 &amp;&amp; ze.getSize() &gt; 0 &amp;&amp; ze.getCrc() != 0.0) { // If size=-1 -&gt; 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)) &gt;= 0) { fout.write(buffer, 0, read); } zin.closeEntry(); fout.close(); } else { Log.v(TAG, "Skipping entry" + ze.getName()); } } } zin.close(); } } </code></pre> <p><strong>A couple of notes</strong></p> <p><strong>1)</strong> I can unzip both files on my windows 7 pc</p> <p><strong>2)</strong> My collegue can unzip both files on his Mac</p> <p><strong>3)</strong> The only problem is, on Android I cannot unzip the zip file the MAC creates...</p> <p><strong>QUESTION:</strong></p> <p>Does anyone know why the zip file that was zipped on a Mac has these invalid sizes? Is my Unzipping process (on Android) missing some code?</p> <p>You can download the zips here if you want, as well as a very small apk to show the output:</p> <p>EDIT: updated the links</p> <p><a href="http://www.haarmodemanowi.nl/stackoverflow/test_mac.zip" rel="nofollow">Zip file (zipped on a Mac)</a></p> <p><a href="http://www.haarmodemanowi.nl/stackoverflow/test_win.zip" rel="nofollow">Zip file (zippen on Win7)</a></p> <p><a href="http://www.haarmodemanowi.nl/stackoverflow/Stackoverflow.apk" rel="nofollow">Demo.apk</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload