Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
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.
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_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,
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(); } } } 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,
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"); 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.
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); } } } 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,
File sd = Environment.getExternalStorageDirectory(); if (sd.canWrite()) { final File backupDBFolder = new File(sd.getPath()); unzip(backupDBPath, backupDBFolder.getPath()); } 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.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
your welcome
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
We've bought a Shopware Extension which is a hybrid App, which we can now offer in the Google Play Store and the IOS App Store. I'am still impressed how fast and how sharp the support team leads us through every process of the App-Store clarification. Very fast and useful response.
Markus Walter
Founder, Das Apartment Living
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.