package com.matthias.android.amginori.persistence;

import android.content.Context;
import android.net.Uri;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class AnkiPackageImporter {

    private static final String ZIP_FILE_ENTRY = "collection.anki2";

    private static final int FILE_COPY_BUFFER_SIZE = 2048;

    public static boolean importAnkiPackage(Context context, String dbName, Uri file) {
        Anki2DbHelper database = new Anki2DbHelper(context);
        database.getReadableDatabase();
        try {
            ZipFile zipFile = new ZipFile(new File(file.getPath()), ZipFile.OPEN_READ);
            ZipEntry ze = zipFile.getEntry(ZIP_FILE_ENTRY);
            if (ze == null || ze.isDirectory()) {
                return false;
            }
            InputStream in = zipFile.getInputStream(ze);
            OutputStream out = new FileOutputStream(context.getDatabasePath(dbName).getAbsolutePath());
            byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
            int n;
            while ((n = in.read(buffer)) > 0) {
                out.write(buffer, 0, n);
            }
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            return false;
        }
        database.close();
        return true;
    }
}

@@ -660,24 +660,89 @@
 Database();%0A
+        ZipFile zipFile = null;%0A        OutputStream out = null;%0A
         try 
@@ -751,32 +751,24 @@
             
-ZipFile 
 zipFile = ne
@@ -1042,37 +1042,24 @@
             
-OutputStream 
 out = new Fi
@@ -1370,18 +1370,23 @@
         
-in
+zipFile
 .close()
@@ -1417,24 +1417,24 @@
 eption e) %7B%0A
-
             
@@ -1439,32 +1439,380 @@
   return false;%0A
+        %7D finally %7B%0A            if (out != null) %7B%0A                try %7B%0A                    out.close();%0A                %7D catch (IOException e) %7B%0A                %7D%0A            %7D%0A            if (zipFile != null) %7B%0A                try %7B%0A                    zipFile.close();%0A                %7D catch (IOException e) %7B%0A                %7D%0A            %7D%0A
         %7D%0A      

Add finally block