vartika
vartika

Reputation: 33

Zip two folders in one folder using java

I am trying to zip two folder with each having some text file. Now I want that 2 folders should zip as one folder with their respective files.

I tried to code, but there is some issue with the zip.

The Zip contains multiple folders.

Here is the code:

package com.test;

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

public class ZipFolders
{
    public static void main(String[] args) throws IOException
    {    
        List<String> listOfDir = new ArrayList<String>();

        String dirpath1 = "/home/administrator/Documents/ZipTest/folder1";
        String dirpath2 = "/home/administrator/Documents/ZipTest/folder2";

        String ZipName = "/home/administrator/Documents/ZipTest/output.zip";
        listOfDir.add(dirpath1);
        listOfDir.add(dirpath2);

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(ZipName));
        zipDirectories(listOfDir,zos); 
        zos.close(); 
        System.out.println("Zip Created Successfully");
    }

    private static void zipDirectories(List<String> listOfDir, ZipOutputStream zos) {
        for(String dirPath:listOfDir){
            try {
                zipdirectory(dirPath, zos);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void zipdirectory(String dirpath, ZipOutputStream zos) throws IOException
    {
        File f = new File(dirpath);
        String[] flist = f.list();
        for(int i=0; i<flist.length; i++)
        {
            File ff = new File(f,flist[i]);
            if(ff.isDirectory())
            {
                zipdirectory(ff.getPath(),zos);    
                continue;
            }
            String filepath = ff.getPath();
            ZipEntry entries = new ZipEntry(filepath);
            zos.putNextEntry(entries);
            FileInputStream fis = new FileInputStream(ff);
            int buffersize = 1024;
            byte[] buffer = new byte[buffersize];
            int count;
            while((count = fis.read(buffer)) != -1)
            {
                zos.write(buffer,0,count);    
            }
            fis.close();
        }
    }
}

But the output I am getting is not as expected I am getting folder 1 & folder 2 like output.zip//home/administrator/Documents/ZipTest/ then here we are having our folder1 & folder2.

My expected output was :- inside the zip only the two folders should exist with there files.

Upvotes: 2

Views: 586

Answers (3)

vartika
vartika

Reputation: 33

Copy , Paste the Code and Run it for the result :-

   package com.test;

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

public class ZipFolders
{
    public static void main(String[] args) throws IOException
    {    
        List<String> listOfDir = new ArrayList<String>();

        String dirpath1 = "/home/administrator/Documents/ZipTest/folder1";
        String dirpath2 = "/home/administrator/Documents/ZipTest/folder2";

        String ZipName = "/home/administrator/Documents/ZipTest/output.zip";
        listOfDir.add(dirpath1);
        listOfDir.add(dirpath2);

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(ZipName));
        zipDirectories(listOfDir,zos); 
        zos.close(); 
        System.out.println("Zip Created Successfully");
    }

    private static void zipDirectories(List<String> listOfDir, ZipOutputStream zos) {
        for(String dirPath:listOfDir){
            try {
                zipdirectory(dirPath, zos);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void zipdirectory(String dirpath, ZipOutputStream zos) throws IOException
    {
        File f = new File(dirpath);
        String[] flist = f.list();
        for(int i=0; i<flist.length; i++)
        {
            File ff = new File(f,flist[i]);
            if(ff.isDirectory())
            {
                zipdirectory(ff.getPath(),zos);    
                continue;
            }
            String fileName = ff.getPath().substring(ff.getPath().lastIndexOf('/'));

            String folder = dirpath.substring(dirpath.lastIndexOf('/')+1);

            ZipEntry entries = new ZipEntry(folder+fileName);
            zos.putNextEntry(entries);
            FileInputStream fis = new FileInputStream(ff);
            int buffersize = 1024;
            byte[] buffer = new byte[buffersize];
            int count;
            while((count = fis.read(buffer)) != -1)
            {
                zos.write(buffer,0,count);    
            }
            fis.close();
        }
    }
}

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 9650

You need to distiquish between the path of each file/dir and the path of the zip entry:

private static void zipDirectories(List<String> listOfDir, ZipOutputStream zos) {
    for(String dirPath:listOfDir){
        try {
            File dir = new File(dirPath);
            zipdirectory(dir, dir.getName(), zos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public static void zipdirectory(File dir, String dirpath,
        ZipOutputStream zos) throws IOException
{
    String[] flist = dir.list();
    for(int i=0; i<flist.length; i++)
    {
        String fn = flist[i];
        String fp = dirpath == null || dirpath.isEmpty()
                ? fn : dirpath + "/" + fn;
        File ff = new File(dir, fn);
        if(ff.isDirectory())
        {
            zipdirectory(ff, fp,zos);
            continue;
        }
        ZipEntry entries = new ZipEntry(fp);
        zos.putNextEntry(entries);
        FileInputStream fis = new FileInputStream(ff);
        int buffersize = 1024;
        byte[] buffer = new byte[buffersize];
        int count;
        while((count = fis.read(buffer)) != -1)
        {
            zos.write(buffer,0,count);    
        }
        fis.close();
    }
}

Upvotes: 0

KYL3R
KYL3R

Reputation: 4073

Your zipdirectory method is used recursive here:

if(ff.isDirectory())
{
     zipdirectory(ff.getPath(),zos);    
     continue;
}

That is creating all the folders you see.

Upvotes: 1

Related Questions