package pl.etroya.corejava.ioStreamsAndFiles;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws URISyntaxException, IOException {
        String[] data = {
                "Line 1",
                "Line 2 2",
                "Line 3 3 3"
        };

        FileSystem zipFs = openZip(Paths.get("testData.zip"));
    }

    private static FileSystem openZip(Path zipPath) throws URISyntaxException, IOException {
        Map<String, String> providerProps = new HashMap<>();
        providerProps.put("create", "true");

        URI zipUri = new URI("jar:file", zipPath.toUri().getPath(), null);
        FileSystem zipFs = FileSystems.newFileSystem(zipUri, providerProps);
        return zipFs;

    }

    private static void copyToZim(FileSystem zipFs) {

    }
}

package pl.etroya.corejava.ioStreamsAndFiles;

import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws URISyntaxException, IOException {
        String[] data = {
                "Line 1",
                "Line 2 2",
                "Line 3 3 3"
        };

        try(FileSystem zipFs = openZip(Paths.get("testData.zip"))){
            copyToZip(zipFs);
            writeToFileInZip1(zipFs, data); //copy file to ZIP traditionally
            writeToFileInZip2(zipFs, data); //copy file to ZIP new way
        } catch (Exception e){
            System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }

    private static FileSystem openZip(Path zipPath) throws URISyntaxException, IOException {
        Map<String, String> providerProps = new HashMap<>();
        providerProps.put("create", "true");

        URI zipUri = new URI("jar:file", zipPath.toUri().getPath(), null);
        FileSystem zipFs = FileSystems.newFileSystem(zipUri, providerProps);
        return zipFs;

    }

    private static void copyToZip(FileSystem zipFs) throws IOException {
        Path sourceFile = Paths.get("file.txt");
        //Path sourceFile = FileSystems.getDefault().getPath("file.txt);
        Path destFile = zipFs.getPath("/fileCopied.txt");

        Files.copy(sourceFile, destFile, StandardCopyOption.REPLACE_EXISTING);

    }

    private static void writeToFileInZip1(FileSystem zipFs, String[] data) throws IOException {
        try(BufferedWriter writer = Files.newBufferedWriter(zipFs.getPath("/newFile1.txt"))) {
            for(String d:data){
                writer.write(d);
                writer.newLine();
            }

        }
    }

    private static void writeToFileInZip2(FileSystem zipFs, String[] data) throws IOException {
        Files.write(zipFs.getPath("/newFile2.txt"), Arrays.asList(data), Charset.defaultCharset(), StandardOpenOption.CREATE);
    }
}

write files to zip in different way