暂时用zip4j解压缩文件.(提取读取但在缓存文件中不可见)

Rya*_*yan 6 java eclipse desktop

我正在使用zip4j和打包和提取工作,但我很好奇如何只是提取文件而不将文件放在缓存中.

这是我在另一个线程上找到的一些代码:

public static void main() {
    String source = "C:\\Users\\gamecaching\\Cache.zip";
    String destination = "C:\\Users\\gamecaching\\";
    String password = "mypassword";

    try {
        ZipFile zipFile = new ZipFile(source);
        if (zipFile.isEncrypted()) {
            zipFile.setPassword(password);
        }
        zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

如何让它仅在程序运行时提取(&&提取的文件在目录中不可见)并在程序退出后删除.

Sri*_*ala 15

创建ZipFile后

ZipFile zipFile = new ZipFile(source);
Run Code Online (Sandbox Code Playgroud)

你可以循环遍历zip文件中的每个文件,如下所示:

ArrayList fileHeaderList = zipFile.getFileHeaders();
Run Code Online (Sandbox Code Playgroud)

对于每个ZipFile:

for (int i = 0; i < fileHeaderList.size(); i++) {
    FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过执行此操作来获取inputStream

is = zipFile.getInputStream(fileHeader);
Run Code Online (Sandbox Code Playgroud)

现在您可以读取InputStream了.

一个详细的例子与流工作如下(取自Zip4j例子包).虽然这个例子还是写入一个文件,但它显示了与zip文件中使用流.我建议看一下示例包中的更多示例.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.ZipInputStream;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.unzip.UnzipUtil;

/**
 * Example demonstrating the use of InputStreams to extract files from the
 * ZipFile
 */
public class ExtractAllFilesWithInputStreams {

    private final int BUFF_SIZE = 4096;

    public ExtractAllFilesWithInputStreams() {
        ZipInputStream is = null;
        OutputStream os = null;
        try {
            // Initiate the ZipFile
            ZipFile zipFile = new ZipFile(
                "C:\\ZipTest\\ExtractAllFilesWithInputStreams.zip");
            String destinationPath = "c:\\ZipTest";
            // If zip file is password protected then set the password
            if (zipFile.isEncrypted()) {
                zipFile.setPassword("password");
            }
            // Get a list of FileHeader. FileHeader is the header information
            // for all the files in the ZipFile
            List fileHeaderList = zipFile.getFileHeaders();
            // Loop through all the fileHeaders
            for (int i = 0; i < fileHeaderList.size(); i++) {
                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
                if (fileHeader != null) {
                    // Build the output file
                    String outFilePath = destinationPath
                        + System.getProperty("file.separator")
                        + fileHeader.getFileName();
                    File outFile = new File(outFilePath);
                    // Checks if the file is a directory
                    if (fileHeader.isDirectory()) {
                        // This functionality is up to your requirements
                        // For now I create the directory
                        outFile.mkdirs();
                        continue;
                    }
                    // Check if the directories(including parent directories)
                    // in the output file path exists
                    File parentDir = outFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    // Get the InputStream from the ZipFile
                    is = zipFile.getInputStream(fileHeader);
                    // Initialize the output stream
                    os = new FileOutputStream(outFile);
                    int readLen = -1;
                    byte[] buff = new byte[BUFF_SIZE];
                    // Loop until End of File and write the contents to the
                    // output stream
                    while ((readLen = is.read(buff)) != -1) {
                        os.write(buff, 0, readLen);
                    }
                    // Please have a look into this method for some important
                    // comments
                    closeFileHandlers(is, os);
                    // To restore File attributes (ex: last modified file time,
                    // read only flag, etc) of the extracted file, a utility
                    // class can be used as shown below
                    UnzipUtil.applyFileAttributes(fileHeader, outFile);
                    System.out.println("Done extracting: "
                        + fileHeader.getFileName());
                } else {
                    System.err.println("fileheader is null. Shouldn't be here");
                }
            }
        } catch (ZipException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                closeFileHandlers(is, os);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void closeFileHandlers(ZipInputStream is, OutputStream os)
            throws IOException {
        // Close output stream
        if (os != null) {
            os.close();
            os = null;
        }
        // Closing inputstream also checks for CRC of the the just extracted
        // file. If CRC check has to be skipped (for ex: to cancel the unzip
        // operation, etc) use method is.close(boolean skipCRCCheck) and set the
        // flag, skipCRCCheck to false
        // NOTE: It is recommended to close outputStream first because Zip4j
        // throws an exception if CRC check fails
        if (is != null) {
            is.close();
            is = null;
        }
    }

    public static void main(String[] args) {
        new ExtractAllFilesWithInputStreams();
    }
}
Run Code Online (Sandbox Code Playgroud)