Question: I have a dynamic text file that picks content from a database according to the user's query. I have to write this content into a text file and zip it in a folder in a servlet. How should I do this?

Answer: 
Here is an example code to compress **a Whole Directory**(including sub files and sub directories), it's using the walk file tree feature of Java NIO.

```
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompress {
    public static void compress(String dirPath) {
        final Path sourceDir = Paths.get(dirPath);
        String zipFileName = dirPath.concat(".zip");
        try {
            final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                    try {
                        Path targetFile = sourceDir.relativize(file);
                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
                        byte[] bytes = Files.readAllBytes(file);
                        outputStream.write(bytes, 0, bytes.length);
                        outputStream.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

```

To use this, just call

```
ZipCompress.compress("target/directoryToCompress");

```

and you'll get a zip file directoryToCompress.zip
I know this question is answered but if you have a list of strings and you want to create a separate file for each string in the archive, you can use the snippet below.

```
public void zipFileTest() throws IOException {

    Map<String, String> map = Map.ofEntries(
            new AbstractMap.SimpleEntry<String, String>("File1.txt", "File1 Content"),
            new AbstractMap.SimpleEntry<String, String>("File2.txt", "File2 Content"),
            new AbstractMap.SimpleEntry<String, String>("File3.txt", "File3 Content")
    );

    createZipFileFromStringContents(map, "archive.zip");

}

public void createZipFileFromStringContents(Map<String, String> map, String zipfilePath) throws IOException {

    FileOutputStream fout = new FileOutputStream(zipfilePath);
    ZipOutputStream zout = new ZipOutputStream(fout);

    for (Map.Entry<String, String> entry : map.entrySet()) {
        String fileName = entry.getKey();
        ZipEntry zipFile = new ZipEntry(fileName);
        zout.putNextEntry(zipFile);
        String fileContent = entry.getValue();
        zout.write(fileContent.getBytes(), 0, fileContent.getBytes().length);
        zout.closeEntry();
    }

    zout.close();
}

```

It will create a zip file with the structure as in the below image:

[![zip file structure](https://i.stack.imgur.com/1QzP6.png)](https://i.stack.imgur.com/1QzP6.png)