src/main/java/de/blau/android/util/FileUtil.java
/**
     * Unpack a zipped file
     * 
     * Code from http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android
     * 
     * @param dir preset directory
     * @param zipname the zip file
     * @return true if successful
     */
Unpack a zipped file



@param dir preset directory
@param zipname the zip file
@return true if successful
Unpack a zipped file @param dir preset directory @param zipname the zip file @return true if successful
public static boolean unpackZip(String dir, String zipname) {
        try (InputStream is = new FileInputStream(dir + zipname); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) {
            String filename;
            ZipEntry ze;
            byte[] buffer = new byte[1024];
            int count;
            while ((ze = zis.getNextEntry()) != null) {
                filename = ze.getName();
                Log.d(DEBUG_TAG, "Unzip " + filename);
                if (!"".equals(filename)) {
                    if (filename.indexOf(PATH_DELIMITER_CHAR) > 0 && !filename.endsWith(Paths.DELIMITER)) { 
                        int slash = filename.lastIndexOf(PATH_DELIMITER_CHAR);
                        String path = filename.substring(0, slash);
                        File fmd = new File(dir + path);
                        if (!fmd.exists()) {
                            fmd.mkdirs();
                        }
                    } else if (ze.isDirectory()) {
                        File fmd = new File(dir + filename);
                        if (!fmd.exists()) {
                            fmd.mkdirs();
                        }
                        continue;
                    }
                    try (FileOutputStream fout = new FileOutputStream(dir + filename)) {
                        while ((count = zis.read(buffer)) != -1) {
                            fout.write(buffer, 0, count);
                        }
                    }
                    zis.closeEntry();
                }
            }
        } catch (IOException e) {
            Log.e(DEBUG_TAG, "Unzipping failed with " + e.getMessage());
            return false;
        }
        return true;
    }
public static boolean unpackZip ( String dir , String zipname ) { try ( InputStream is = new FileInputStream ( dir + zipname ) ; ZipInputStream zis = new ZipInputStream ( new BufferedInputStream ( is ) ) ) { String filename ; ZipEntry ze ; byte [ ] buffer = new byte [ 1024 ] ; int count ; while ( ( ze = zis . getNextEntry ( ) ) != null ) { filename = ze . getName ( ) ; Log . d ( DEBUG_TAG , "Unzip " + filename ) ; if ( ! "" . equals ( filename ) ) { if ( filename . indexOf ( PATH_DELIMITER_CHAR ) > 0 && ! filename . endsWith ( Paths . DELIMITER ) ) { int slash = filename . lastIndexOf ( PATH_DELIMITER_CHAR ) ; String path = filename . substring ( 0 , slash ) ; File fmd = new File ( dir + path ) ; if ( ! fmd . exists ( ) ) { fmd . mkdirs ( ) ; } } else if ( ze . isDirectory ( ) ) { File fmd = new File ( dir + filename ) ; if ( ! fmd . exists ( ) ) { fmd . mkdirs ( ) ; } continue ; } try ( FileOutputStream fout = new FileOutputStream ( dir + filename ) ) { while ( ( count = zis . read ( buffer ) ) != - 1 ) { fout . write ( buffer , 0 , count ) ; } } zis . closeEntry ( ) ; } } } catch ( IOException e ) { Log . e ( DEBUG_TAG , "Unzipping failed with " + e . getMessage ( ) ) ; return false ; } return true ; }
Unpack a zipped file
Unpack a zipped file
// zapis do souboru // Need to create directories if not exists, or // it will generate an Exception... // NOSONAR // noinspection ResultOfMethodCallIgnored // cteni zipu a zapis