Java examples for File Path IO:Zip File
ZIP filesystem provider handles ZIP and JAR files as though they were filesystems.
You can easily access the contents of the file.
You can manipulate the file as you would do ordinary files, including copying, deleting, moving, and renaming the file.
You can modify certain attributes of the file.
import java.io.IOException; import java.net.URI; import java.nio.file.DirectoryStream; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<String, String> attributes = new HashMap<>(); attributes.put("create", "true"); try {//from ww w .j ava 2s . c o m URI zipFile = URI.create("jar:file:/home.zip"); try (FileSystem zipFileSys = FileSystems.newFileSystem(zipFile, attributes);) { Path path = zipFileSys.getPath("docs"); Files.createDirectory(path); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(zipFileSys.getPath("/"));) { for (Path file : directoryStream) { System.out.println(file.getFileName()); } } } } catch (IOException e) { e.printStackTrace(); } } }
OpenOption interface specifies how the file is opened and the StandardOpenOption enumeration implements this interface.
The values of the enumeration are summarized in the following table:
Enumeration | Meaning |
---|---|
APPEND | Bytes are written to the end of the file |
CREATE | Creates a new file if it does not exist |
CREATE_NEW | Creates a new file only if the file does not exist |
DELETE_ON_CLOSE | Deletes the file when it is closed |
DSYNC | Every update to a file is written synchronously |
READ | Open for read access |
SPARSE | Sparse file |
SYNC | Every update to the file or metadata is written synchronously |
TRUNCATE_EXISTING | Truncates the length of a file to 0 when opening a file |
WRITE | Opens the file for write access |