本文整理汇总了Java中net.lingala.zip4j.core.ZipFile.getInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java ZipFile.getInputStream方法的具体用法?Java ZipFile.getInputStream怎么用?Java ZipFile.getInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.lingala.zip4j.core.ZipFile
的用法示例。
在下文中一共展示了ZipFile.getInputStream方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extract
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
private byte[] extract(ZipFile zipFile, String file, boolean isFileMandatory) throws ZipException, IOException {
// Files in the zip are flattened, so remove the folder prefix if there is one
String fileName = getFileNameFromPath(file);
// Extract from zip
FileHeader header = zipFile.getFileHeader(fileName);
if (header == null) {
if (isFileMandatory) {
// Mandatory file not found - throw exception
throw new IllegalArgumentException(String.format("File %s missing from model run outputs", fileName));
} else {
// Optional file not found - return null
return null;
}
} else {
try (ZipInputStream inputStream = zipFile.getInputStream(header)) {
return IOUtils.toByteArray(inputStream);
}
}
}
示例2: writeZipEntryBytes
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
public static void writeZipEntryBytes(File zipFile, String zipEntryPath, OutputStream outputStream)
throws IOException {
TFile trueZipFile = new TFile(new File(zipFile, zipEntryPath));
try {
try {
InputStream inputStream;
if (trueZipFile.isArchive()) {
synchronizeQuietly(trueZipFile);
ZipFile internalZipFile = new ZipFile(zipFile);
inputStream = internalZipFile.getInputStream(internalZipFile.getFileHeader(zipEntryPath));
} else {
inputStream = new TFileInputStream(trueZipFile);
}
IoUtil.copy(inputStream, outputStream);
} catch (ZipException e) {
throw new IOException("Can't write ZIP-entry bytes.", e);
}
} finally {
IoUtil.closeQuietly(outputStream);
synchronizeQuietly(trueZipFile);
}
}
示例3: loadMap
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
/**
* Tries to load the map data for a name
*
* @param name the name of the map to load
* @return the loaded map
* @throws MapException when
*/
@Nonnull
public Map loadMap(@Nonnull String name) {
Optional<Map> map = getMap(name);
if (!map.isPresent()) {
Optional<MapInfo> mapInfo = getMapInfo(name);
if (!mapInfo.isPresent()) {
throw new MapException(
"Unknown map " + name + ". Did you register it into the world config?");
}
try {
ZipFile zipFile = new ZipFile(new File(worldsFolder, mapInfo.get().getName() + ".zip"));
for (FileHeader header : (List<FileHeader>) zipFile.getFileHeaders()) {
if (header.getFileName().endsWith("config.json")) {
InputStream stream = zipFile.getInputStream(header);
Map m = gson.fromJson(new JsonReader(new InputStreamReader(stream)), Map.class);
m.initMarkers(mapHandler);
maps.add(m);
return m;
}
}
throw new MapException("Could not load map config for map " + name
+ ". Fileheader was null. Does it has a map.json?");
} catch (Exception e) {
throw new MapException("Error while trying to load map config " + name, e);
}
} else {
return map.get();
}
}
示例4: getInputStream
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
public static ZipInputStream getInputStream(File source, String fileName) {
try {
ZipFile zipFile = new ZipFile(source);
return zipFile.getInputStream(zipFile.getFileHeader(fileName));
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
示例5: loadMap
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
/**
* Tries to load the map data for a name
*
* @param name the name of the map to load
* @return the loaded map
* @throws MapException when
*/
@Nonnull
public Map loadMap(@Nonnull String name) {
Optional<Map> map = getMap(name);
if (!map.isPresent()) {
if (!getMapInfo(name).isPresent()) {
throw new MapException("Unknown map " + name + ". Did you register it into the world config?");
}
try {
ZipFile zipFile = new ZipFile(new File(worldsFolder, name + ".zip"));
for (FileHeader header : (List<FileHeader>) zipFile.getFileHeaders()) {
if (header.getFileName().endsWith("config.json")) {
InputStream stream = zipFile.getInputStream(header);
Map m = gson.fromJson(new JsonReader(new InputStreamReader(stream)), Map.class);
maps.add(m);
return m;
}
}
throw new MapException("Could not load map config for map " + name + ". Fileheader was null. Does it has a map.json?");
} catch (Exception e) {
throw new MapException("Error while trying to load map config " + name, e);
}
} else {
return map.get();
}
}
示例6: extract
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
@Override
public InputStream extract(File zip, String fileName, String password) throws ZipException {
try {
ZipFile zipFile = new ZipFile(zip);
if ((password != null) && zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
FileHeader fh = zipFile.getFileHeader(fileName);
return zipFile.getInputStream(fh);
} catch (net.lingala.zip4j.exception.ZipException e) {
throw new ZipException(e);
}
}
示例7: ExtractSelectFilesWithInputStream
import net.lingala.zip4j.core.ZipFile; //导入方法依赖的package包/类
public ExtractSelectFilesWithInputStream() {
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 the FileHeader of the File you want to extract from the
//zip file. Input for the below method is the name of the file
//For example: 123.txt or abc/123.txt if the file 123.txt
//is inside the directory abc
FileHeader fileHeader = zipFile.getFileHeader("yourfilename");
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();
return;
}
//Check if the directories(including parent directories)
//in the output file path exists
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs(); //If not create those directories
}
//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);
}
//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
is.close();
//Close output stream
os.close();
//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 does not exist");
}
} catch (Exception e) {
e.printStackTrace();
}
}