From 3408d3565a7e34c7d961a5639ff0b1a506d9d96e Mon Sep 17 00:00:00 2001 From: Hammy Date: Fri, 25 Dec 2020 04:57:41 +0000 Subject: [PATCH] Create Zip File --- .../src/zip/file/system/Main.java | 62 ++++++++++++++++++ .../src/zip/file/system/myData.zip | Bin 0 -> 680 bytes 2 files changed, 62 insertions(+) create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/Main.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/myData.zip diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/Main.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/Main.java new file mode 100644 index 0000000..19723ec --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/Main.java @@ -0,0 +1,62 @@ +package zip.file.system; + +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.Collections; +import java.util.Map; + +public class Main { + private static final String root = "C:\\Users\\sgoud\\JavaProjects\\Learning-Java\\Pluralsite\\src\\intermediate\\Java Fundamentals - The Core Platform\\src\\zip\\file\\system\\"; + private static final String rootResources = "C:\\Users\\sgoud\\JavaProjects\\Learning-Java\\Pluralsite\\src\\intermediate\\Java Fundamentals - The Core Platform\\resources\\"; + + public static void main(String[] args) { + String[] data = { + "Line 1", + "Line 2 2", + "Line 3 3 3", + "Line 4 4 4 4", + "Line 5 5 5 5 5" + }; + + try (FileSystem zipFs = openZip(Paths.get(root + "myData.zip"))) { + copyToZip(zipFs); + writeToFileInZip1(zipFs, data); + writeToFileInZip2(zipFs, data); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static FileSystem openZip(Path zipPath) throws IOException, URISyntaxException { + Map providerProperties = Collections.singletonMap("create", "true"); + + URI zipURI = new URI("jar:file", zipPath.toUri().getPath(), null); + return FileSystems.newFileSystem(zipURI, providerProperties); + } + + private static void copyToZip(FileSystem zipFs) throws IOException { + // Path sourceFile = FileSystems.getDefault().getPath("file1.txt"); + Path sourceFile = Paths.get(rootResources + "file1.txt"); + Path destinationFile = zipFs.getPath("/file1Copied.txt"); + + Files.copy(sourceFile, destinationFile, 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); + } +} \ No newline at end of file diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/myData.zip b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/zip/file/system/myData.zip new file mode 100644 index 0000000000000000000000000000000000000000..0dd947eb9323e5d6eae2986e944ca31f0bc30625 GIT binary patch literal 680 zcmWIWW@Zs#;Nak3P*R^6$bba+8B`e3GILT5o%0JaQ&aRxDoVH*6u=4?86+4EaEql> z-MYqzP8XLHl@*p2fB*31)3=XbKl5|&aPe{Sa`$)iFfjypvvVj_d|xdCG(`%C18`c- z1GGFZwcHJ4Kf28iA|W=T)1ObC@eVb3^7IqqMK>