Last active
September 18, 2016 20:52
-
-
Save trplll/6a4ec46c79e1a9aab0233f6d33e463b4 to your computer and use it in GitHub Desktop.
Using Java.nio.file , java.io , and java util to create and modify directories, zip directories , and files in the file system
This file contains hidden or 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.FileSystem; | |
import java.nio.file.FileSystems; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardCopyOption; | |
import java.nio.file.StandardOpenOption; | |
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()); | |
} | |
} | |
private static FileSystem openZip(Path zipPath) throws IOException, URISyntaxException { | |
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("file1.txt"); | |
//Path sourceFile = FileSystems.getDefault().getPath("file1.txt"); | |
Path destFile = zipFs.getPath("/file1Copied.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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment