Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract specific file in a zip file in java

Tags:

java

zip

I need to provide a view of zip file to customer in system, and allow customers download choosed files.

  1. parse the zip file and show on the web page. and remember every zipentry location(for example file1 is starting from byte 100 width length 1024bytes) in backend.
  2. download the specified file when customer click the download button.

now I have rememberred all zipentry locations, but is there java zip tools to unzip the specified location of zip files?? API just like unzip(file, long entryStart, long entryLength);

like image 504
Mr rain Avatar asked Aug 24 '15 09:08

Mr rain


People also ask

Can I extract one file from a ZIP?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How do you extract parts of a zip file?

Right-click on any of the zip files that are a part of the collection and click on the "Extract here" or "Extract to folder" option in the pop-up menu. Your zip application will load up and begin decompressing all the files. A progression bar will appear on the screen and once it is fully loaded, it will fade away.

How do I extract a Java zip file?

To unzip a zip file, we need to read the zip file with ZipInputStream and then read all the ZipEntry one by one. Then use FileOutputStream to write them to file system. We also need to create the output directory if it doesn't exists and any nested directories present in the zip file.

How can I read the content of a Zip file without unzipping it in Java?

Methods. getComment(): String – returns the zip file comment, or null if none. getEntry(String name): ZipEntry – returns the zip file entry for the specified name, or null if not found. getInputStream(ZipEntry entry) : InputStream – Returns an input stream for reading the contents of the specified zip file entry.


2 Answers

This can be done without messing with byte arrays or input streams using Java 7's NIO2:

public void extractFile(Path zipFile, String fileName, Path outputFile) throws IOException {
    // Wrap the file system in a try-with-resources statement
    // to auto-close it when finished and prevent a memory leak
    try (FileSystem fileSystem = FileSystems.newFileSystem(zipFile, null)) {
        Path fileToExtract = fileSystem.getPath(fileName);
        Files.copy(fileToExtract, outputFile);
    }
}
like image 163
Thunderforge Avatar answered Sep 17 '22 13:09

Thunderforge


You can try like this:

ZipFile zf = new ZipFile(file);
try {
  InputStream in = zf.getInputStream(zf.getEntry("file.txt"));
  // ... read from 'in' as normal
} finally {
  zf.close();
}

I havent tried it but in Java 7 ZipFileSystem you can try like this to extract file.TXT file from the zip file.

Path zipfile = Paths.get("/samples/ziptest.zip");
FileSystem fs = FileSystems.newFileSystem(zipfile, env, null);
final Path root = fs.getPath("/file.TXT");
like image 39
Garry Avatar answered Sep 16 '22 13:09

Garry