當前位置: 首頁>>代碼示例>>Java>>正文


Java ZipFile.getFileHeader方法代碼示例

本文整理匯總了Java中net.lingala.zip4j.core.ZipFile.getFileHeader方法的典型用法代碼示例。如果您正苦於以下問題:Java ZipFile.getFileHeader方法的具體用法?Java ZipFile.getFileHeader怎麽用?Java ZipFile.getFileHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.lingala.zip4j.core.ZipFile的用法示例。


在下文中一共展示了ZipFile.getFileHeader方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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);
        }
    }
}
 
開發者ID:SEEG-Oxford,項目名稱:ABRAID-MP,代碼行數:21,代碼來源:MainHandler.java

示例2: verifyBackupFile

import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
public static boolean verifyBackupFile(File file) {
    try {
        ZipFile zipFile = new ZipFile(file);
        FileHeader preferences = zipFile.getFileHeader(BACKUP_PREFERENCES_PATH);
        FileHeader notificationRules = zipFile.getFileHeader(BACKUP_NOTIFICATION_RULES_PATH);
        FileHeader commandAliases = zipFile.getFileHeader(BACKUP_COMMAND_ALIASES_PATH);
        return preferences != null && notificationRules != null && commandAliases != null;
    } catch (ZipException e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:MCMrARM,項目名稱:revolution-irc,代碼行數:13,代碼來源:BackupManager.java

示例3: isBackupPasswordProtected

import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
public static boolean isBackupPasswordProtected(File file) {
    try {
        ZipFile zipFile = new ZipFile(file);
        FileHeader preferences = zipFile.getFileHeader(BACKUP_PREFERENCES_PATH);
        return preferences.isEncrypted();
    } catch (ZipException e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:MCMrARM,項目名稱:revolution-irc,代碼行數:11,代碼來源:BackupManager.java

示例4: hasFile

import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
 * 是否包含指定文件
 *
 * @param zip
 * @param fileName
 * @return
 */
public static boolean hasFile(ZipFile zip, String fileName){
    try {
        return zip.getFileHeader(fileName) != null;
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return false;
}
 
開發者ID:linchaolong,項目名稱:ApkToolPlus,代碼行數:16,代碼來源:ZipUtils.java

示例5: readManufacturerFiles

import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
 * Read the files of a manufacturer.
 *
 * @param zip The ZIP file to read from
 * @param manufacturerKey The zip file key of the manufacturer, e.g. "M-0004"
 * @throws ZipException
 */
void readManufacturerFiles(ZipFile zip, String manufacturerKey) throws ZipException
{
   LOGGER.info("Reading files of manufacturer {}", manufacturerKey);

   String catalogFileName = manufacturerKey + "/Catalog.xml";
   FileHeader catalogFileHeader = zip.getFileHeader(catalogFileName);
   if (catalogFileHeader == null)
   {
      LOGGER.info("Skipping {}: no catalog file {} found", manufacturerKey, catalogFileName);
      return;
   }

   String hardwareFileName = manufacturerKey + "/Hardware.xml";
   FileHeader hardwareFileHeader = zip.getFileHeader(hardwareFileName);
   if (hardwareFileHeader == null)
   {
      LOGGER.info("Skipping {}: no hardware file {} found", manufacturerKey, hardwareFileName);
      return;
   }

   LOGGER.info("Reading {}", catalogFileName);
   KnxDocument catalogDocument = readXml(zip.getInputStream(catalogFileHeader), catalogFileName);

   LOGGER.info("Reading {}", hardwareFileName);
   KnxDocument hardwareDocument = readXml(zip.getInputStream(hardwareFileHeader), hardwareFileName);

   // Process the product files of the manufacturer
   String filePrefix = manufacturerKey + '/' + manufacturerKey + '_';
   @SuppressWarnings("unchecked")
   List<FileHeader> fileHeaders = zip.getFileHeaders();
   for (FileHeader fileHeader : fileHeaders)
   {
      if (fileHeader.getFileName().startsWith(filePrefix))
      {
         LOGGER.info("Reading {}", fileHeader.getFileName());
         KnxDocument productsDocument = readXml(zip.getInputStream(fileHeader), fileHeader.getFileName());
      }
   }
}
 
開發者ID:selfbus,項目名稱:tools-libraries,代碼行數:47,代碼來源:KnxprodReader.java

示例6: removeDirFromZipArchive

import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
 * 刪除壓縮文件中的目錄
 * 
 * @param file
 *            目標zip文件
 * @param removeDir
 *            要移除的目錄
 * @param charset
 *            編碼
 * @throws ZipException
 */
@SuppressWarnings("rawtypes")
private static void removeDirFromZipArchive(String file, String removeDir,
		String charset) throws ZipException {
	// 創建ZipFile並設置編碼
	ZipFile zipFile = new ZipFile(file);

	if (null != charset || !"".equals(charset)) {
		zipFile.setFileNameCharset(charset);
	}

	// 給要刪除的目錄加上路徑分隔符
	if (!removeDir.endsWith(File.separator))
		removeDir += File.separator;

	// 如果目錄不存在, 直接返回
	FileHeader dirHeader = zipFile.getFileHeader(removeDir);
	if (null == dirHeader)
		return;

	// 遍曆壓縮文件中所有的FileHeader, 將指定刪除目錄下的子文件名保存起來
	List headersList = zipFile.getFileHeaders();
	List<String> removeHeaderNames = new ArrayList<String>();
	for (int i = 0, len = headersList.size(); i < len; i++) {
		FileHeader subHeader = (FileHeader) headersList.get(i);
		if (subHeader.getFileName().startsWith(dirHeader.getFileName())
				&& !subHeader.getFileName().equals(dirHeader.getFileName())) {
			removeHeaderNames.add(subHeader.getFileName());
		}
	}
	// 遍曆刪除指定目錄下的所有子文件, 最後刪除指定目錄(此時已為空目錄)
	for (String headerNameString : removeHeaderNames) {
		zipFile.removeFile(headerNameString);
	}
	zipFile.removeFile(dirHeader);
}
 
開發者ID:v5developer,項目名稱:maven-framework-project,代碼行數:47,代碼來源:CompressUtil.java

示例7: 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);
    }
}
 
開發者ID:brightgenerous,項目名稱:brigen-base,代碼行數:14,代碼來源:ZipDelegaterZip4j.java

示例8: 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();
	}
}
 
開發者ID:joielechong,項目名稱:Zip4jAndroid,代碼行數:80,代碼來源:ExtractSelectFilesWithInputStream.java

示例9: activateApp

import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
private void activateApp(App app)
{
	FileMeta appSourceArchive = app.getSourceFiles();
	if (appSourceArchive != null)
	{
		File fileStoreFile = fileStore.getFile(appSourceArchive.getId());
		if (fileStoreFile == null)
		{
			LOG.error("Source archive '{}' for app '{}' missing in file store", appSourceArchive.getId(),
					app.getName());
			throw new RuntimeException("An error occurred trying to activate app");
		}

		try
		{
			ZipFile zipFile = new ZipFile(fileStoreFile);
			if (!app.getUseFreemarkerTemplate())
			{
				FileHeader fileHeader = zipFile.getFileHeader("index.html");
				if (fileHeader == null)
				{
					LOG.error(
							"Missing index.html in {} while option Use freemarker template as index.html was set 'No'",
							app.getName());
					throw new RuntimeException(
							format("Missing index.html in %s while option 'Use freemarker template as index.html' was set 'No'",
									app.getName()));
				}
			}
			//noinspection StringConcatenationMissingWhitespace
			zipFile.extractAll(
					fileStore.getStorageDir() + separatorChar + FILE_STORE_PLUGIN_APPS_PATH + separatorChar
							+ app.getId() + separatorChar);
		}
		catch (ZipException e)
		{
			LOG.error("", e);
			throw new RuntimeException(format("An error occurred activating app '%s'", app.getName()));
		}
	}
}
 
開發者ID:molgenis,項目名稱:molgenis,代碼行數:42,代碼來源:AppRepositoryDecorator.java


注:本文中的net.lingala.zip4j.core.ZipFile.getFileHeader方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。