1

I am able to zip files contained in a particular folder. This is the code that I have used:

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'm calling this class in this way in another class:

String[] files = {mainFolderPath+"/text1.txt", mainFolderPath+ "/text2.txt", mainFolderPath +"/NewFolder"};
        Compress compress = new Compress(files, sourceFile.getAbsolutePath());
        compress.zip();

On running the application I'm getting an IOException.

Could you please tell me how to zip the "NewFolder" which contains another text file along with the text files "text1.txt" and "text2.txt"?

Thank you.

Ingrid Cooper
  • 1,191
  • 3
  • 16
  • 27
  • What line is throwing the exception? Have you done any test on reading / writing files? Maybe you just don't have permission to access the filesystem? – enTropy Sep 24 '12 at 11:08
  • 1
    Thank you for your reply. This is the line that throws an IOException if I include the "NewFolder" in the String[] files array. If I don't add the "NewFolder" I am able to get the zipped folder containing only the "text1.txt" and "text2.txt". ** while ((count = origin.read(data, 0, BUFFER)) != -1) ** – Ingrid Cooper Sep 24 '12 at 11:12

2 Answers2

0

It works for me!!!!!!!! in all my projects I need....

    public String[] _files;
    public String _zipFile  = "/mnt/sdcard/123.zip"; 



    /////zIIIiiiiPPPPPer

 public class Compress { 
 private static final int BUFFER = 2048; 
 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)); 
     String path = "/mnt/sdcard/out";
     File fileDir = new File( path );
     if(!fileDir.exists() || !fileDir.isDirectory())
     {
         return;
     }
     String[] _files = fileDir.list(); 
     for ( int i = 0 ; i < _files.length ; i++  )
     { 
           _files[i] = path + "/"+ _files[i];
     }
     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(); 
   } 

 }   

To call it JuSt do smth like...

Compress cs = new Compress(_files, _zipFile);
timonvlad
  • 1,046
  • 3
  • 13
  • 31
  • Thank you for your reply. I used the above code but its still giving me the same exception ie; java.io.IOException: Is a directory after encountering the "NewFolder". – Ingrid Cooper Sep 24 '12 at 11:52
  • Do you have in manifest? – timonvlad Sep 24 '12 at 11:52
  • No, actually I'm not saving the zip in the sdcard and neither are the "NewFolder" and "text1.txt" and "text2.txt" files in the sdcard. The zip folder is saved in the data/data/packagename/cache folder and the "NewFolder" and 2 text files are in data/data/packagename/cache/Main folder. – Ingrid Cooper Sep 24 '12 at 11:57
  • =))) So, your problem is to save files in Internal memory ... Look http://stackoverflow.com/questions/5766609/save-internal-file-in-my-own-internal-folder-in-android – timonvlad Sep 24 '12 at 12:01
  • The text files and "New folder" are saving in the path data/data/packagename/cache/Main folder. If I zip only the 2 text files they are getting zipped and the zipped folder is saving in the path data/data/packagename/cache/temp.zip where "temp.zip" is my zipped folder. But if I include the "New folder" along with the 2 text files it shows the IOException: Is a directory and doesn't write anything in the "temp.zip" folder. – Ingrid Cooper Sep 24 '12 at 12:38
  • Your internal memory is not RW maybe... That's why you can't create New_folder... Try chmod command to folder... – timonvlad Sep 24 '12 at 13:08
  • The temp.zip is having the "RW" permission. – Ingrid Cooper Sep 24 '12 at 13:17
  • An error occures when you trying to zip folder in folder) look at it http://stackoverflow.com/questions/5414925/how-to-zip-folder-recursively-in-android – timonvlad Sep 24 '12 at 14:39
  • If you are creating a folder, at first zip it and then a temp.zip with *.txt and *.zip.... So you'll have folder in /dat/dat/com.*** only with files, but not with files and folders! – timonvlad Sep 24 '12 at 14:42
0

I got the solution for my question ie; zipping only the contents of a particular folder which includes a folder along with some text files. This is the code:

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]);
            String name =  _files[i];
            File f = new File( _files[i]);
            if (f.isDirectory()) {
                name = name.endsWith("/") ? name : name + "/";

                for (String file : f.list()) {
                    System.out.println(" checking " + file);
                    System.out
                            .println("The folder name is: " + f.getName());
                    out.putNextEntry(new ZipEntry(f.getName() + "/" + file));
                    FileInputStream fi = new FileInputStream( _files[i]
                            + "/" + file);
                    origin = new BufferedInputStream(fi, BUFFER);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                    origin.close();
                }
                System.out.println(" checking folder" + name);
            } else {
                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();
    }
}

}
Ingrid Cooper
  • 1,191
  • 3
  • 16
  • 27