public static void zipFiles( List<File> filenames, String zipFile, String replacedRootPath, ICallbackNotifier callback) throws IOException { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[Constants.BUFFER]; int fileCount = 1; int total = filenames.size(); Log.d(TAG, "Start zipping to: " + zipFile); for (File file : filenames) { String absPath = file.getAbsolutePath(); String message = LNReaderApplication.getInstance() .getApplicationContext() .getResources() .getString(R.string.zip_files_task_progress_count, fileCount, total, absPath); Log.d(TAG, message); if (callback != null) callback.onCallBack(new CallbackEventData(message)); FileInputStream fi = new FileInputStream(file); origin = new BufferedInputStream(fi, Constants.BUFFER); ZipEntry entry = new ZipEntry(absPath.replace(replacedRootPath, "")); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, Constants.BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); ++fileCount; } out.close(); Log.d(TAG, "Completed zipping to: " + zipFile); }
public static void unzipFiles(String zipName, String rootPath, ICallbackNotifier callback) throws FileNotFoundException, IOException { InputStream is = new FileInputStream(zipName); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; byte[] buffer = new byte[Constants.BUFFER]; int count; Log.d(TAG, "Start unzipping: " + zipName + " to: " + rootPath); // create root path File root = new File(rootPath); root.mkdirs(); int fileCount = 1; while ((ze = zis.getNextEntry()) != null) { String filename = rootPath + ze.getName(); // Need to create directories if not exists, or // it will generate an Exception... if (ze.isDirectory()) { Log.d(TAG, "Creating dir1: " + filename); File fmd = new File(filename); fmd.mkdirs(); continue; } // check if target dir exist String dir = filename.substring(0, filename.lastIndexOf("/")); File rootDir = new File(dir); if (!rootDir.exists()) { Log.d(TAG, "Creating dir2: " + dir); rootDir.mkdirs(); } Log.d(TAG, "Unzipping: " + filename); if (callback != null) { String message = LNReaderApplication.getInstance() .getApplicationContext() .getResources() .getString(R.string.unzip_files_task_progress_count, fileCount, filename); callback.onCallBack(new CallbackEventData(message)); } FileOutputStream fout = new FileOutputStream(filename); while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeEntry(); ++fileCount; } zis.close(); Log.d(TAG, "Completed unzipping to: " + zipName); }