Updated 21 February 2018
In this blog, we will learn that how to zip and unzip your file or folder programmatically.
A computer file whose contents are compressed for storage or transmission. A ZIP file, like other archive file formats, is simply a collection of one or more files and/or folders but is compressed into a single file for easy transportation and compression.
These permissions are required to store data in your device storage.
12 <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
create a simple zip file,
12345678910111213141516171819202122232425262728 public class ZipManager {private static int BUFFER_SIZE = 6 * 1024;public static void zip(String[] files, String zipFile) throws IOException {BufferedInputStream origin = null;ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));try {byte data[] = new byte[BUFFER_SIZE];for (int i = 0; i < files.length; i++) {FileInputStream fi = new FileInputStream(files[i]);origin = new BufferedInputStream(fi, BUFFER_SIZE);try {ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));out.putNextEntry(entry);int count;while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {out.write(data, 0, count);}} finally {origin.close();}}} finally {out.close();}}}
how to use it, Simple call this function,
1234567 String backupDBPath = Environment.getExternalStorageDirectory().getPath() + "/Mobikul-Pos-db-backup";final File backupDBFolder = new File(backupDBPath);backupDBFolder.mkdirs();final File backupDB = new File(backupDBFolder, "/db_pos.db");String[] s = new String[1];s[0] = backupDB.getAbsolutePath();zip(s, backupDBPath + "/pos_demo.zip");
now you have created a zip file in the Mobikul-Pos-db-backup folder.
123456789101112131415161718192021222324252627282930313233343536373839404142 public class ZipManager {private static int BUFFER_SIZE = 6 * 1024;public static void unzip(String zipFile, String location) throws IOException {try {File f = new File(location);if (!f.isDirectory()) {f.mkdirs();}ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));try {ZipEntry ze = null;while ((ze = zin.getNextEntry()) != null) {String path = location + File.separator + ze.getName();if (ze.isDirectory()) {File unzipFile = new File(path);if (!unzipFile.isDirectory()) {unzipFile.mkdirs();}} else {FileOutputStream fout = new FileOutputStream(path, false);try {for (int c = zin.read(); c != -1; c = zin.read()) {fout.write(c);}zin.closeEntry();} finally {fout.close();}}}} finally {zin.close();}} catch (Exception e) {e.printStackTrace();Log.e(TAG, "Unzip exception", e);}}}
simply call above function,
12345 File sd = Environment.getExternalStorageDirectory();if (sd.canWrite()) {final File backupDBFolder = new File(sd.getPath());unzip(backupDBPath, backupDBFolder.getPath());}
Now you have successfully unzipped your file in external storage.
I hope this blog is helpful for you. If you have any doubt please ask in comments.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments
your welcome