Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress files into zip folder in android? [duplicate]

Tags:

I am trying to send multiple files in email, i got the codes for attaching them in the email.

   Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);                        sendIntent.setType("application/octet-stream");   ArrayList<Uri> uris = new ArrayList<Uri>();                        uris.add(0, Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/.file"));     File f = getActivity().getDatabasePath(".db");     Log.i(TAG,"DB path"+ f.getAbsolutePath());     uris.add(1, Uri.fromFile(f));     sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 

But now, i need to zip them into a single zip file and then i have to send them in the email. Well i saw many answers and ideas but still i can't get some clear ideas. Can you guys help me to solve this problem.

like image 908
Vicky Avatar asked Aug 29 '14 05:08

Vicky


People also ask

Can I zip a folder twice?

You can do that, you shouldn't see any problem but compressing a compressed folder, you won't gain a lot of compression.

How do I compress multiple files into a zip file?

Right-click on the file or folder. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

Can you double zip a file to make it smaller?

Unfortunately, there isn't a simple method to make a ZIP file smaller. Once you squeeze the files to their minimum size, you can't squeeze them again. So zipping a zipped file won't do anything, and on some occasions, it can make the size even bigger.


1 Answers

These persmissions are required to store data to your device storage.
Mainfest.xml file

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Permission requested for write external storage(Updated by 2018/10/04)

 public static void verifyStoragePermissions(Activity activity) {     // Check if we have write permission     int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);      if (permission != PackageManager.PERMISSION_GRANTED) {         // We don't have permission so prompt the user         ActivityCompat.requestPermissions(                 activity,                 PERMISSIONS_STORAGE,                 REQUEST_EXTERNAL_STORAGE         );     } } 

Zip Function

public void zip(String[] _files, String zipFileName) {         try {             BufferedInputStream origin = null;             FileOutputStream dest = new FileOutputStream(zipFileName);             ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(                     dest));             byte data[] = new byte[BUFFER];              for (int i = 0; i < _files.length; 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();         }     } 

Use the Zip Function

String[] s = new String[2];  // Type the path of the files in here s[0] = inputPath + "/image.jpg"; s[1] = inputPath + "/textfile.txt"; // /sdcard/ZipDemo/textfile.txt  // first parameter is d files second parameter is zip file name ZipManager zipManager = new ZipManager();  // calling the zip function zipManager.zip(s, inputPath + inputFile); 

Unzip Function

public void unzip(String _zipFile, String _targetLocation) {          //create target location folder if not exist         dirChecker(_targetLocatioan);          try {             FileInputStream fin = new FileInputStream(_zipFile);             ZipInputStream zin = new ZipInputStream(fin);             ZipEntry ze = null;             while ((ze = zin.getNextEntry()) != null) {                  //create dir if required while unzipping                 if (ze.isDirectory()) {                     dirChecker(ze.getName());                 } else {                     FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());                     for (int c = zin.read(); c != -1; c = zin.read()) {                         fout.write(c);                     }                      zin.closeEntry();                     fout.close();                 }              }             zin.close();         } catch (Exception e) {             System.out.println(e);         } } 

Use the unzip function

ZipManager zipManager = new ZipManager(); zipManager.unzip(inputPath + inputFile, outputPath); 

[Source: http://stacktips.com/tutorials/android/how-to-programmatically-zip-and-unzip-file-in-android]

like image 78
B M Avatar answered Sep 23 '22 01:09

B M