Created
September 16, 2018 02:08
-
-
Save rupert-ong/1aa04253d2553ac3d25c5a86cc1293d1 to your computer and use it in GitHub Desktop.
Java Creating ZIP file system and Copying and Writing into It #java #filesystem #streams #buffered #files #io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ABCDEFGHIJKLMNOPQRSTUVWXYZ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jwhh; | |
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) { | |
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("myData.zip"))) { | |
copyToZip(zipFs); | |
writeToFileInZip1(zipFs, data); | |
writeToFileInZip2(zipFs, data); | |
} catch (Exception e) { | |
System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage()); | |
} | |
} | |
/** | |
* Creates a ZIP file system (Specialized File System, not default) | |
* | |
* @param zipPath Path instance representing the ZIP file | |
* @return custom ZIP FileSystem | |
* @throws IOException | |
* @throws URISyntaxException | |
*/ | |
private static FileSystem openZip(Path zipPath) throws IOException, URISyntaxException { | |
// Create properties for file system provider | |
Map<String, String> providerProps = new HashMap<>(); | |
providerProps.put("create", "true"); | |
// Identify file systems by creating a URI | |
// ZIP using a scheme of jar:file | |
URI zipUri = new URI("jar:file", zipPath.toUri().getPath(), null); | |
// Create FileSystem | |
return FileSystems.newFileSystem(zipUri, providerProps); | |
} | |
private static void copyToZip(FileSystem zipFs) throws IOException { | |
// Get path from default file system of source file (shorthand) | |
Path sourceFile = Paths.get("file1.txt"); | |
// Longhand way of getting a path within the default file system | |
// Path sourceFile = FileSystems.getDefault().getPath("file1.txt"); | |
// Create a path within our custom file system for the destination file | |
Path destFile = zipFs.getPath("/file1Copied.txt"); | |
Files.copy(sourceFile, destFile, StandardCopyOption.REPLACE_EXISTING); | |
} | |
/** | |
* Traditional way to write into a file. Create BufferedWriter from Files factory method, | |
* iterate through data and write into it line by line | |
* @param zipFs Custom file system | |
* @param data Array of string data | |
* @throws IOException | |
*/ | |
private static void writeToFileInZip1(FileSystem zipFs, String[] data) throws IOException { | |
try (BufferedWriter writer = Files.newBufferedWriter(zipFs.getPath("/newFile1.txt"), StandardOpenOption.CREATE)) { | |
for (String d : data) { | |
writer.write(d); | |
writer.newLine(); | |
} | |
} | |
} | |
/** | |
* Convenience way of writing into a file. Specify path, iterable object (a list), default charset | |
* and options for how file is opened or created | |
* @param zipFs Custom file system | |
* @param data Array of string data | |
* @throws IOException | |
*/ | |
private static void writeToFileInZip2(FileSystem zipFs, String[] data) throws IOException { | |
Files.write(zipFs.getPath("/newFile2.txt"), Arrays.asList(data), Charset.defaultCharset(), StandardOpenOption.CREATE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment