Java 临时使用zip4j提取文件。(为读取而提取,但在缓存文件中不可见)

Java 临时使用zip4j提取文件。(为读取而提取,但在缓存文件中不可见),java,eclipse,desktop,Java,Eclipse,Desktop,我正在使用zip4j&打包和解压缩工作,但我很好奇如何只解压缩文件而不将文件放入缓存 下面是我在另一个线程上找到的一些代码: public static void main() { String source = "C:\\Users\\gamecaching\\Cache.zip"; String destination = "C:\\Users\\gamecaching\\"; String password = "mypassword"; try {

我正在使用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();
    }
}

如何使其仅在程序运行时提取(&&extracted files not visible in directory)并在程序退出后删除

要读取zip文件的具体条目,请使用:
zipFile.getInputStream(FileHeader)
。 要读取所有条目:

for (FileHeader entry: zipFile:getFileHeaders()){
    zipFile.getInputStream(FileHeader);
}

创建ZipFile之后

ZipFile zipFile = new ZipFile(source);
您可以按如下方式循环浏览zip文件中的每个文件:

ArrayList fileHeaderList = zipFile.getFileHeaders();
对于每个ZipFile:

for (int i = 0; i < fileHeaderList.size(); i++) {
    FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
现在您有了要读取的InputStream

下面是一个使用Streams的综合示例(摘自)。尽管此示例仍然写入文件,但它演示了如何在zip文件中使用Streams。我建议在示例包中再看几个示例

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();
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.util.List;
导入net.lingala.zip4j.core.ZipFile;
导入net.lingala.zip4j.exception.zipeexception;
导入net.lingala.zip4j.io.ZipInputStream;
导入net.lingala.zip4j.model.FileHeader;
导入net.lingala.zip4j.unzip.UnzipUtil;
/**
*示例演示如何使用InputStreams从
*拉链
*/
公共类ExtractAllFilesWithInputStreams{
私人最终整容BUFF_SIZE=4096;
public ExtractAllFilesWithInputStreams(){
ZipInputStream=null;
OutputStream os=null;
试一试{
//启动拉链
ZipFile ZipFile=新ZipFile(
“C:\\ZipTest\\extractalfileswithInputStreams.zip”);
字符串destinationPath=“c:\\ZipTest”;
//如果zip文件受密码保护,则设置密码
if(zipFile.isEncrypted()){
zipFile.setPassword(“密码”);
}
//获取FileHeader的列表。FileHeader是标头信息
//对于ZipFile中的所有文件
List fileHeaderList=zipFile.getFileHeaders();
//循环遍历所有文件头
对于(int i=0;i
从ExtractAllFilesWithInputS派生的示例
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();
    }
}
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.ZipInputStream;
import net.lingala.zip4j.model.FileHeader;

public class StackoverFlow18974389 {

    public static void main(String[] args) {
        Map<String, InputStream> inMemoryFiles = new HashMap<>();
        try {
            ZipFile zipFile = new ZipFile("test.zip");
            List fileHeaderList = zipFile.getFileHeaders();
            for (int i = 0; i < fileHeaderList.size(); i++) {
                FileHeader fileHeader = (FileHeader) fileHeaderList.get(i);
                ZipInputStream is = zipFile.getInputStream(fileHeader);
                int uncompressedSize = (int) fileHeader.getUncompressedSize();
                OutputStream os = new ByteArrayOutputStream(uncompressedSize);
                int bytesRead;
                byte[] buffer = new byte[4096];
                while ((bytesRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                byte[] uncompressedBytes = ((ByteArrayOutputStream) os).toByteArray();
                inMemoryFiles.put(fileHeader.getFileName(), new ByteArrayInputStream(uncompressedBytes));
                is.close();
            }
        } catch (ZipException | IOException ex) {
            ex.printStackTrace(System.err);
        }
        // very simple example how to access the files from the map
        for (String fileName : inMemoryFiles.keySet()) {
            System.out.print("in memory file: " + fileName);
            InputStream is = new BufferedInputStream(inMemoryFiles.get(fileName));
            long length = 0;
            try {
                while (is.read() != -1) {
                    length++;
                }
            } catch (IOException ex) {
                ex.printStackTrace(System.err);
            }
            System.out.println(" size. " + length);
        }
    }
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
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 org.apache.commons.io.output.NullOutputStream;

/**
 * Example demonstrating testing Zip files using net.lingala.zip4j
 * 
 * Changes: 
 *      - Created static method testZipFile
 *      - Uses Buffered IO
 *      - Uses Apache Commons' NullOutputStream() for /dev/null
 *      - Uses enhanced for loop
 *      - Skip CRC check
 *      - Removed unapplicable code
 */
public class Zip4jTestZipFile
{
    public static void main(String[] args)
    {
        File zipfile = new File("D:\\JDeveloper\\mywork\\Gas_Market_Systems\\GMS-FLISTCopy\\test\\inetpub\\tmp.lem\\FLIST_COUNTRYNWO_20140411.zip");
        testZipFile(zipfile);
    }

    /* Adapted from SrikanthLingala's answer (http://stackoverflow.com/a/18974782/1213722) */
    public static void testZipFile(File zipfile) {
        ZipInputStream is = null;
        OutputStream os = new NullOutputStream();   // org.apache.commons.io.output.NullOutputStream

        /* Buffered IO */
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(os);

        try {
            // Initiate the ZipFile
            ZipFile zipFile = new ZipFile(zipfile);
            // 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<FileHeader> fileHeaderList = zipFile.getFileHeaders();
            // Loop through all the fileHeaders
            for (FileHeader fileHeader : fileHeaderList) {
                if (fileHeader != null) {

                    // Removed: Build the output file
                    // Removed: directory checks
                    // Removed: parent directory checks

                    // Get the InputStream from the ZipFile
                    is = zipFile.getInputStream(fileHeader);
                    bis = new BufferedInputStream(is);

                    // Removed: Initialize the output stream

                    int readLen = -1;
                    // Loop until End of File and write the contents to the
                    // output stream
                    while ((readLen = bis.read()) != -1)
                    {
                        bos.write(readLen);
                    }

                    // Please have a look into this method for some important
                    // comments
                    closeFileHandlers(is, os);

                    // Removed: restore File attributes

                    System.out.println("Done testing: "
                        + 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 static 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();
            boolean skipCRCCheck = false;
            is.close(skipCRCCheck);
            is = null;
        }
    }
}