0

I am having my app to export the db table / rows as CSV file. But Currently I need to tweak a bit of the implementation.

  1. I am using opencsv library (CSV writer/Reader) to export and import csv file.

  2. After i create the csv file, I need to again zip it programatiicaly and create the .zip file to be exported

  3. After that I need to create a password for the zip file.

Please help to know how to create a zip file from the csv file contents, on the fly.

Also how can i set a password for the zip file created.

Kindly help me to achieve point 2 and 3 .

Sunil
  • 521
  • 1
  • 7
  • 24

1 Answers1

0
public class Compress { 
  private static final int BUFFER = 2048; 

  private String[] _files; 
  private String _zipFile; 

  public Compress(String[] files, String zipFile) { 
    _files = files; 
    _zipFile = zipFile; 
  } 

  public void zip() { 
    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.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(); 
    } 

  } 

}

I have not tried this in android but hope it will worked in android. Hope this help you.

Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
  • Ithanks for the reply. but this is for using java.util.zip class. I like the approach as i can use as input stream. But still the main issue is password protection. I need to create zip file and set a password for it. If you can suggest ways, it will help me to implement export and import – Sunil Mar 23 '16 at 13:50