org.apache.poi.poifs.filesystem.FileMagic类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(1421)

本文整理了Java中org.apache.poi.poifs.filesystem.FileMagic类的一些代码示例,展示了FileMagic类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileMagic类的具体详情如下:
包路径:org.apache.poi.poifs.filesystem.FileMagic
类名称:FileMagic

FileMagic介绍

[英]The file magic number, i.e. the file identification based on the first bytes of the file
[中]文件幻数,即基于文件第一个字节的文件标识

代码示例

代码示例来源:origin: org.apache.poi/poi

public VBAMacroReader(InputStream rstream) throws IOException {
  InputStream is = FileMagic.prepareToCheckMagic(rstream);
  FileMagic fm = FileMagic.valueOf(is);
  if (fm == FileMagic.OLE2) {
    fs = new POIFSFileSystem(is);
  } else {
    openOOXML(is);
  }
}

代码示例来源:origin: org.apache.poi/poi

public static FileMagic valueOf(byte[] magic) {
  for (FileMagic fm : values()) {
    for (byte[] ma : fm.magic) {
      if (findMagic(ma, magic)) {
        return fm;
      }
    }
  }
  return UNKNOWN;
}

代码示例来源:origin: alibaba/easyexcel

public static ExcelTypeEnum valueOf(InputStream inputStream){
    try {
      if (!inputStream.markSupported()) {
        return null;
      }
      FileMagic fileMagic =  FileMagic.valueOf(inputStream);
      if(FileMagic.OLE2.equals(fileMagic)){
        return XLS;
      }
      if(FileMagic.OOXML.equals(fileMagic)){
        return XLSX;
      }
      return null;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: looly/hutool

/**
   * 是否为XLSX格式的Excel文件(XSSF)<br>
   * XLSX文件主要用于Excel 2007+创建
   * 
   * @param in excel输入流
   * @return 是否为XLSX格式的Excel文件(XSSF)
   */
  public static boolean isXlsx(InputStream in) {
    if (false == in.markSupported()) {
      in = new BufferedInputStream(in);
    }
    try {
      return FileMagic.valueOf(in) == FileMagic.OOXML;
    } catch (IOException e) {
      throw new IORuntimeException(e);
    }
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Opens the specified stream as a secure zip
 *
 * @param stream
 *            The stream to open.
 * @return The zip stream freshly open.
 */
@SuppressWarnings("resource")
public static ZipArchiveThresholdInputStream openZipStream(InputStream stream) throws IOException {
  // Peek at the first few bytes to sanity check
  InputStream checkedStream = FileMagic.prepareToCheckMagic(stream);
  verifyZipHeader(checkedStream);
  
  // Open as a proper zip stream
  return new ZipArchiveThresholdInputStream(new ZipArchiveInputStream(checkedStream));
}

代码示例来源:origin: looly/hutool

/**
   * 是否为XLSX格式的Excel文件(XSSF)<br>
   * XLSX文件主要用于Excel 2007+创建
   * 
   * @param in excel输入流
   * @return 是否为XLSX格式的Excel文件(XSSF)
   */
  public static boolean isXlsx(InputStream in) {
    if (false == in.markSupported()) {
      in = new BufferedInputStream(in);
    }
    try {
      return FileMagic.valueOf(in) == FileMagic.OOXML;
    } catch (IOException e) {
      throw new IORuntimeException(e);
    }
  }
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.poi

/**
 * Opens the specified stream as a secure zip
 *
 * @param stream
 *            The stream to open.
 * @return The zip stream freshly open.
 */
@SuppressWarnings("resource")
public static ZipArchiveThresholdInputStream openZipStream(InputStream stream) throws IOException {
  // Peek at the first few bytes to sanity check
  InputStream checkedStream = FileMagic.prepareToCheckMagic(stream);
  verifyZipHeader(checkedStream);
  
  // Open as a proper zip stream
  return new ZipArchiveThresholdInputStream(new ZipArchiveInputStream(checkedStream));
}

代码示例来源:origin: org.apache.poi/poi

/**
 * @return does this ObjectData have an associated POIFS Directory Entry?
 * (Not all do, those that don't have a data portion)
 */
default boolean hasDirectoryEntry() {
  try (final InputStream is = FileMagic.prepareToCheckMagic(getInputStream())) {
    FileMagic fm = FileMagic.valueOf(is);
    return fm == FileMagic.OLE2;
  } catch (IOException e) {
    POILogger LOG = POILogFactory.getLogger(ObjectData.class);
    LOG.log(POILogger.WARN, "Can't determine filemagic of ole stream", e);
    return false;
  }
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Get the file magic of the supplied {@link File}<p>
 *
 * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
 *  that the ZIP stream has leading junk bytes
 *
 * @param inp a file to be identified
 */
public static FileMagic valueOf(final File inp) throws IOException {
  try (FileInputStream fis = new FileInputStream(inp)) {
    final byte[] data = IOUtils.toByteArray(fis, 8);
    return FileMagic.valueOf(data);
  }
}

代码示例来源:origin: openl-tablets/openl-tablets

@Override
public ExcelReader create(String fileName, final InputStream is) throws ExcelParseException {
  boolean useFile = fileName != null;
  if (useFile && is != null) {
    throw new IllegalArgumentException("Only one argument can be non-null");
  }
  InputStream tempStream = null;
  try {
    tempStream = FileMagic.prepareToCheckMagic(useFile ? new FileInputStream(fileName) : is);
    // Opening the file by name is preferred because using an InputStream has a higher memory footprint than using a File
    if (isXlsx(tempStream)) {
      return useFile ? new SAXReader(fileName) : new SAXReader(tempStream);
    } else {
      return useFile ? new EventReader(fileName) : new EventReader(tempStream);
    }
  } catch (IOException e) {
    throw new ExcelParseException(e);
  } finally {
    IOUtils.closeQuietly(tempStream);
  }
}
/**

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.poi

public static FileMagic valueOf(byte[] magic) {
  for (FileMagic fm : values()) {
    for (byte[] ma : fm.magic) {
      if (findMagic(ma, magic)) {
        return fm;
      }
    }
  }
  return UNKNOWN;
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Verifies that the given stream starts with a Zip structure.
 * 
 * Warning - this will consume the first few bytes of the stream,
 *  you should push-back or reset the stream after use!
 */
private static void verifyZipHeader(InputStream stream) throws NotOfficeXmlFileException, IOException {
  InputStream is = FileMagic.prepareToCheckMagic(stream);
  FileMagic fm = FileMagic.valueOf(is);
  switch (fm) {
  case OLE2:
    throw new OLE2NotOfficeXmlFileException(
      "The supplied data appears to be in the OLE2 Format. " +
      "You are calling the part of POI that deals with OOXML "+
      "(Office Open XML) Documents. You need to call a different " +
      "part of POI to process this data (eg HSSF instead of XSSF)");
  case XML:
    throw new NotOfficeXmlFileException(
      "The supplied data appears to be a raw XML file. " +
      "Formats such as Office 2003 XML are not supported");
  default:
    // Don't check for a Zip header, as to maintain backwards
    //  compatibility we need to let them seek over junk at the
    //  start before beginning processing.
    break;
  }
}

代码示例来源:origin: org.apache.poi/poi

/**
   * Checks that the supplied InputStream (which MUST
   *  support mark and reset) has a OOXML (zip) header at the start of it.<p>
   *  
   * If unsure if your InputStream does support mark / reset,
   *  use {@link FileMagic#prepareToCheckMagic(InputStream)} to wrap it and make
   *  sure to always use that, and not the original!
   *  
   * @param inp An InputStream which supports either mark/reset
   *
   * @deprecated in 3.17-beta2, use {@link FileMagic#valueOf(InputStream)} == FileMagic.OOXML instead
   */
  @Deprecated
  @Removal(version="4.0")
  public static boolean hasOOXMLHeader(InputStream inp) throws IOException {
    return FileMagic.valueOf(inp) == FileMagic.OOXML;
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

@Override
public boolean hasDirectoryEntry() {
  InputStream is = null;
  try {
    is = getObjectPart().getInputStream();
    is = FileMagic.prepareToCheckMagic(is);
    return FileMagic.valueOf(is) == FileMagic.OLE2;
  } catch (IOException e) {
    LOG.log(POILogger.WARN, "can't determine if directory entry exists", e);
    return false;
  } finally {
    IOUtils.closeQuietly(is);
  }
}

代码示例来源:origin: org.apache.poi/poi

/**
 * Get the file magic of the supplied InputStream (which MUST
 *  support mark and reset).<p>
 *
 * If unsure if your InputStream does support mark / reset,
 *  use {@link #prepareToCheckMagic(InputStream)} to wrap it and make
 *  sure to always use that, and not the original!<p>
 *
 * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
 *  that the ZIP stream has leading junk bytes
 *
 * @param inp An InputStream which supports either mark/reset
 */
public static FileMagic valueOf(InputStream inp) throws IOException {
  if (!inp.markSupported()) {
    throw new IOException("getFileMagic() only operates on streams which support mark(int)");
  }
  // Grab the first 8 bytes
  byte[] data = IOUtils.peekFirst8Bytes(inp);
  return FileMagic.valueOf(data);
}

代码示例来源:origin: org.apache.poi/poi

InputStream is = FileMagic.prepareToCheckMagic(inp);
FileMagic fm = FileMagic.valueOf(is);

代码示例来源:origin: looly/hutool

/**
 * 是否为XLS格式的Excel文件(HSSF)<br>
 * XLS文件主要用于Excel 97~2003创建
 * 
 * @param in excel输入流
 * @return 是否为XLS格式的Excel文件(HSSF)
 */
public static boolean isXls(InputStream in) {
  final PushbackInputStream pin = IoUtil.toPushbackStream(in, 8);
  try {
    return FileMagic.valueOf(pin) == FileMagic.OLE2;
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: org.apache.poi/poi

P extends TextParagraph<S,P,? extends TextRun>
> SlideShow<S,P> create(InputStream inp, String password) throws IOException, EncryptedDocumentException {
  InputStream is = FileMagic.prepareToCheckMagic(inp);
  FileMagic fm = FileMagic.valueOf(is);

代码示例来源:origin: looly/hutool

/**
 * 是否为XLS格式的Excel文件(HSSF)<br>
 * XLS文件主要用于Excel 97~2003创建
 * 
 * @param in excel输入流
 * @return 是否为XLS格式的Excel文件(HSSF)
 */
public static boolean isXls(InputStream in) {
  final PushbackInputStream pin = IoUtil.toPushbackStream(in, 8);
  try {
    return FileMagic.valueOf(pin) == FileMagic.OLE2;
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

public static POITextExtractor createExtractor(InputStream inp) throws IOException, OpenXML4JException, XmlException {
  InputStream is = FileMagic.prepareToCheckMagic(inp);
  FileMagic fm = FileMagic.valueOf(is);
  
  switch (fm) {
  case OLE2:
    POIFSFileSystem fs = new POIFSFileSystem(is);
    boolean isEncrypted = fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY); 
    return isEncrypted ? createEncryptedOOXMLExtractor(fs) : createExtractor(fs);
  case OOXML:
    return createExtractor(OPCPackage.open(is));
  default:
    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
  }
}

相关文章

微信公众号

最新文章

更多