How to pin files in android

I have a requirement to programmatically zip text files.

I created text files in directory(context.getFilesDirectory()) files directory(context.getFilesDirectory()) ,
I want to zip text files and add a zipped file to an intent object.

Please provide me with a piece of code on how to pin files in android.

+4
source share
3 answers

If you have a FOLDER in SDCard and you want to create its zip code, just copy and paste this code into your project and it will provide you with a ZIP folder. This code will create a zip code in a folder that contains only files, subfolders should not be inside. You can further change for yourself.

  String []s=new String[2]; //declare an array for storing the files ie the path of your source files s[0]="/mnt/sdcard/Wallpaper/pic.jpg"; //Type the path of the files in here s[1]="/mnt/sdcard/Wallpaper/Final.pdf"; // path of the second file zip((s,"/mnt/sdcard/MyZipFolder.zip"); //call the zip function public void zip(String[] files, String zipFile) { private String[] _files= files; private String _zipFile= zipFile; try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(_zipFile); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; for(int i=0; i < _files.length; i++) { Log.d("add:",_files[i]); Log.v("Compress", "Adding: " + _files[i]); FileInputStream fi = new FileInputStream(_files[i]); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch(Exception e) { e.printStackTrace(); } 

}

Also add permissions to android-manifest.xml with this code

  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> 
+6
source

If you want to compress files with a password, you can look at this library , you need to add these lines to your build.gradle:

 dependencies { compile 'com.github.ghost1372:Mzip-Android:0.4.0' } 

here is the code for zip files:

Zip:

 ZipArchive zipArchive = new ZipArchive(); zipArchive.zip(targetPath,destinationPath,password); 
0
source

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

  <!-- == External Storage == --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
0
source

All Articles