How to compress the contents of a directory in a zip?

2

Hello everyone.

I have asked this question so that you could give me a hand in the obstacle that has been presented to me on my way.

My problem

My problem is that I want to be able to compress the data or the contents of a folder in a zip, but my current compression code has not given me a positive or expected result. For the reason that it does not compress the desired thing

My code

public class Comp {
  public static void zipDir(OutputStream zipFilename, String dir) throws Exception {
        File dirObj = new File(dir);
        ZipOutputStream out = new ZipOutputStream(zipFilename);
        System.out.println("Creating : ");
        addDir(dirObj, out);
        out.close();
      }

      static void addDir(File dirObj, ZipOutputStream out) throws IOException {
        File[] files = dirObj.listFiles();
        byte[] tmpBuf = new byte[1024];

        for (int i = 0; i < files.length; i++) {
          if (files[i].isDirectory()) {
            addDir(files[i], out);
            continue;
          }
          FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
          System.out.println(" Adding: " + files[i].getAbsolutePath());
          out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
          int len;
          while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
          }
          out.closeEntry();
          in.close();
        }
      }

     }

Explanation: My code is very simple, I just pass two parameters, one that is the ouputStream where it can be written and the other is where I passed the path of the folder where I copied the content

And I call these methods from the activity where the compression process will be triggered in the following way:

    File file = new File(bb);
    OutputStream os = driveContents1.getOutputStream();
    Comp comp=new Comp();
    try {
        comp.zipDir(os, file.getAbsolutePath());
        Toast.makeText(this, "Se COmprimimio", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

If you notice where I fail in my code or know other ways to achieve my goal. Please communicate it to me. Thanks.

    
asked by Abraham.P 12.02.2017 в 20:39
source

1 answer

1

Your file is empty because you recursively search for the files (which are not there) but you never make entries for folders. I leave you an example that works (it's not production code, you should check the integrity of the zip produced).

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author snolde
 *
 */
public class Zipper {

    File zip;
    ZipOutputStream output;

    public Zipper(File zip) throws FileNotFoundException{
        this.output = new ZipOutputStream(new FileOutputStream(zip));
    }

    private boolean zipFile(File file){
        try {
            byte[] buf = new byte[1024];
            output.putNextEntry(new ZipEntry(file.getPath()));
            FileInputStream fis = new FileInputStream(file);
              int len;
              while ((len = fis.read(buf)) > 0) {
                output.write(buf, 0, len);
              }
              fis.close();
              output.closeEntry();
              return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    private boolean zipDir(File file) {
        try {
            output.putNextEntry(new ZipEntry(file.getPath()+File.pathSeparator));
            output.closeEntry();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    private boolean add(File... files){
        for (File file : files){
            if (file.isDirectory()){
                zipDir(file);
                add(file.listFiles());
            } else {
                zipFile(file);
            }
        }
        return true;
    }

    private void zip(File... files) throws IOException{
        add(files);
        output.finish();
        output.close();
    }

    public static void main(String[] args) {
        try {
            Zipper z = new Zipper(new File("source.zip"));
            z.zip(new File("src"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    
answered by 31.03.2017 / 08:49
source