2

Below is the code I tried. it is zipping only content of the sub folder but not the folder I need to zip all sub folders along with the contents. what i am doing wrong here. Example: I have a folder 'demo' inside demo folder i have a, b, c folders. I want to zip a, b, c along with contents and zip should not include parent that is demo.

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].getName());
          out.putNextEntry(new ZipEntry(files[i].getName()));
          int len;
          while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
          }
          out.closeEntry();
          in.close();
        }
      }

    public static void zipFiles(File[] subd) {

        String zipFile = "test.zip";

        try {

            FileOutputStream fos = new FileOutputStream(zipFile);

            ZipOutputStream zos = new ZipOutputStream(fos);

            for (File dir : subd) {

                addDir(dir, zos);

            }

            // close the ZipOutputStream
            zos.close();

        } catch (IOException ioe) {
            System.out.println("Error creating zip file: " + ioe);
        }
tala d
  • 109
  • 2
  • 7
  • 1
    Maybe something like [this](https://stackoverflow.com/questions/51833423/how-to-zip-the-content-of-a-directory-in-java/51833575#51833575) as a starting point (`out.putNextEntry(new ZipEntry(files[i].getName()));` needs to contain a path element, which is described in the linked example) – MadProgrammer Jul 11 '19 at 03:22
  • if I add path, it adds complete obsolute path, i just need all sub folders and its contents inside the zip. I have explained with example – tala d Jul 11 '19 at 03:38
  • Read the linked example - it only adds relative paths (based on the source path) – MadProgrammer Jul 11 '19 at 03:46
  • Are you talking about this line String name = srcFile.getPath(); name = name.replace(rootPath.getPath(), ""); – tala d Jul 11 '19 at 04:07
  • That’s the start of the basic idea (and just one approach) – MadProgrammer Jul 11 '19 at 04:08
  • Also why he has passed srcFolder two times in the argument, i am bit unclear – tala d Jul 11 '19 at 04:08
  • It's a recursive method, meaning that `addFolderToZip(srcFolder, srcFolder, zip);` is just seeding the initial pass – MadProgrammer Jul 11 '19 at 04:14

1 Answers1

0

Here is a code I did for zip files and directories with hierarchy and without hierarchy. zip.getHierarchy().equals("F") part does the work you want to do. In this code, you have to pass source file, destination, hierarchy (T/F) as arguments. Additionally you may need a DTO class.

public static void Start() throws IOException{ 

    FileOutputStream fos = new FileOutputStream(zip.getDestination());
    ZipOutputStream zipOut = new ZipOutputStream(fos);
    File fileToZip = new File(zip.getSourcePath());

        zipDirectory(fileToZip, fileToZip.getName(), zipOut);

    zipOut.close();
    fos.close();
}

public static void zipDirectory(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {

    if (fileToZip.isDirectory()) {

        File[] children = fileToZip.listFiles();

        for (File childFile : children) {
            if(zip.getHierarchy().equals("T")) {
                zipDirectory(childFile, childFile.getPath(), zipOut);
            }else if(zip.getHierarchy().equals("F")){
                zipDirectory(childFile, childFile.getName(), zipOut);
            }
        }
        return; 
    }
    doZip(fileToZip,fileName,zipOut);
}

public static void doZip(File fileToZip,String fileName,ZipOutputStream zipOut) throws IOException {
    FileInputStream fis = new FileInputStream(fileToZip);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zipOut.putNextEntry(zipEntry);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zipOut.write(bytes, 0, length);
    }
    fis.close();
}
Indi
  • 421
  • 9
  • 28