This is working code for zip files. You must add all the paths to the file that you want to archive to arrayList, and send it as a parameter below the function along with the string name for the zip file you want.
public String zipper(ArrayList<String> allFiles, String zipFileName) throws IOException { timeStampOfZipFile =new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()); App.mediaStorageDir.mkdirs(); zippath = App.mediaStorageDir.getAbsolutePath() + "/" + zipFileName+ timeStampOfZipFile + ".zip"; try { if (new File(zippath).exists()) { new File(zippath).delete(); } //new File(zipFileName).delete(); // Delete if exists ZipFile zipFile = new ZipFile(zippath); ZipParameters zipParameters = new ZipParameters(); zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipParameters.setPassword("Reset"); if (allFiles.size() > 0) { for (String fileName : allFiles) { File file = new File(fileName); zipFile.addFile(file, zipParameters); } } } catch (ZipException e) { e.printStackTrace(); } return zippath; }
Here App.mediaStorageDir.mkdirs(); where mediaStorage is the static end line in my App.class
public static final File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "yourAppFoler");
creates the directory in which the zip file will be saved. The result of the function returns the path to the zip file, which can be used to attach to the multipart object to send it to the server (if you want).
Runtime permission required for API> = marshmellow
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
source share