gpt4 book ai didi

org.apache.commons.compress.archivers.zip.ZipEncoding类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 13:07:07 26 4
gpt4 key购买 nike

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

ZipEncoding介绍

[英]An interface for encoders that do a pretty encoding of ZIP filenames.

There are mostly two implementations, one that uses java.nio java.nio.charset.Charset and one implementation, which copes with simple 8 bit charsets, because java-1.4 did not support Cp437 in java.nio.

The main reason for defining an own encoding layer comes from the problems with java.lang.String#getBytes(String), which encodes unknown characters as ASCII quotation marks ('?'). Quotation marks are per definition an invalid filename on some operating systems like Windows, which leads to ignored ZIP entries.

All implementations should implement this interface in a reentrant way.
[中]一个用于编码器的接口,可以对ZIP文件名进行漂亮的编码。
主要有两种实现,一种使用java。nio java。尼奥。查塞特。字符集和一个实现,它处理简单的8位字符集,因为java-1.4不支持java中的Cp437。尼奥。
定义自己的编码层的主要原因来自java的问题。lang.String#getBytes(String),它将未知字符编码为ASCII引号(“?”)。根据定义,引号在某些操作系统(如Windows)上是无效的文件名,这会导致忽略ZIP条目。
所有的实现都应该以可重入的方式实现这个接口。

代码示例

代码示例来源:origin: org.apache.commons/commons-compress

if (comm != null && !"".equals(comm)) {
  final boolean commentEncodable = zipEncoding.canEncode(comm);
    final ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
    ze.addExtraField(new UnicodeCommentExtraField(comm,
                           commentB.array(),

代码示例来源:origin: org.apache.commons/commons-compress

/**
   * Decodes a byte array to a string.
   */
  static String decode(final ZipEncoding encoding, final byte[] b, final int offset, final int len)
    throws IOException {
    return encoding.decode(Arrays.copyOfRange(b, offset, offset + len));
  }
}

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * Encodes the given string using the configured encoding.
 *
 * @param str the String to write
 * @throws IOException if the string couldn't be written
 * @return result of encoding the string
 */
private byte[] encode(final String str) throws IOException {
  final ByteBuffer buf = zipEncoding.encode(str);
  final int len = buf.limit() - buf.position();
  return Arrays.copyOfRange(buf.array(), buf.arrayOffset(), buf.arrayOffset() + len);
}

代码示例来源:origin: org.apache.commons/commons-compress

private ZipEncoding getEntryEncoding(final ZipArchiveEntry ze) {
  final boolean encodable = zipEncoding.canEncode(ze.getName());
  return !encodable && fallbackToUTF8
    ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
}

代码示例来源:origin: org.apache.commons/commons-compress

throws IOException {
int len = name.length();
ByteBuffer b = encoding.encode(name);
while (b.limit() > length && len > 0) {
  b = encoding.encode(name.substring(0, --len));

代码示例来源:origin: org.apache.commons/commons-compress

private void writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased) throws IOException {
  final boolean encodable = zipEncoding.canEncode(ze.getName());
  final ByteBuffer name = getName(ze);
  if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) {
    addUnicodeExtraFields(ze, encodable, name);
  }
  final long localHeaderStart = streamCompressor.getTotalBytesWritten();
  final byte[] localHeader = createLocalFileHeader(ze, name, encodable, phased, localHeaderStart);
  metaData.put(ze, new EntryMetaData(localHeaderStart, usesDataDescriptor(ze.getMethod(), phased)));
  entry.localDataStart = localHeaderStart + LFH_CRC_OFFSET; // At crc offset
  writeCounted(localHeader);
  entry.dataStart = streamCompressor.getTotalBytesWritten();
}

代码示例来源:origin: org.apache.commons/commons-compress

final ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
final int nameLen = name.limit() - name.position();
final int commentLen = commentB.limit() - commentB.position();
final boolean encodable = zipEncoding.canEncode(ze.getName());
putShort(versionNeededToExtract(zipMethod, needsZip64Extra, entryMetaData.usesDataDescriptor),
  buf, CFH_VERSION_NEEDED_OFFSET);

代码示例来源:origin: org.apache.commons/commons-compress

private ByteBuffer getName(final ZipArchiveEntry ze) throws IOException {
  return getEntryEncoding(ze).encode(ze.getName());
}

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * Parse an entry name from a buffer.
 * Parsing stops when a NUL is found
 * or the buffer length is reached.
 *
 * @param buffer The buffer from which to parse.
 * @param offset The offset into the buffer from which to parse.
 * @param length The maximum number of bytes to parse.
 * @param encoding name of the encoding to use for file names
 * @since 1.4
 * @return The entry name.
 * @throws IOException on error
 */
public static String parseName(final byte[] buffer, final int offset,
                final int length,
                final ZipEncoding encoding)
  throws IOException {
  int len = 0;
  for (int i = offset; len < length && buffer[i] != 0; i++) {
    len++;
  }
  if (len > 0) {
    final byte[] b = new byte[len];
    System.arraycopy(buffer, offset, b, 0, len);
    return encoding.decode(b);
  }
  return "";
}

代码示例来源:origin: org.apache.commons/commons-compress

&& !ASCII.canEncode(entryName)) {
paxHeaders.put("path", entryName);
&& !ASCII.canEncode(linkName)) {
paxHeaders.put("linkpath", linkName);

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

if (comm != null && !"".equals(comm)) {
  final boolean commentEncodable = zipEncoding.canEncode(comm);
    final ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
    ze.addExtraField(new UnicodeCommentExtraField(comm,
                           commentB.array(),

代码示例来源:origin: org.apache.commons/commons-compress

final String paxHeaderName, final byte linkType, final String fieldName)
throws IOException {
final ByteBuffer encodedName = zipEncoding.encode(name);
final int len = encodedName.limit() - encodedName.position();
if (len >= TarConstants.NAMELEN) {

代码示例来源:origin: org.apache.commons/commons-compress

private String readCString(final int length) throws IOException {
  // don't include trailing NUL in file name to decode
  final byte tmpBuffer[] = new byte[length - 1];
  readFully(tmpBuffer, 0, tmpBuffer.length);
  this.in.read();
  return zipEncoding.decode(tmpBuffer);
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

private ZipEncoding getEntryEncoding(final ZipArchiveEntry ze) {
  final boolean encodable = zipEncoding.canEncode(ze.getName());
  return !encodable && fallbackToUTF8
    ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

final ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
final int nameLen = name.limit() - name.position();
final int commentLen = commentB.limit() - commentB.position();
final boolean encodable = zipEncoding.canEncode(ze.getName());
putShort(versionNeededToExtract(zipMethod, needsZip64Extra, entryMetaData.usesDataDescriptor),
  buf, CFH_VERSION_NEEDED_OFFSET);

代码示例来源:origin: org.apache.commons/commons-compress

final ByteBuffer data = this.zipEncoding.encode(comment);
final int dataLen = data.limit() - data.position();
writeCounted(ZipShort.getBytes(dataLen));

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * If the stored CRC matches the one of the given name, return the
 * Unicode name of the given field.
 *
 * <p>If the field is null or the CRCs don't match, return null
 * instead.</p>
 */
private static
  String getUnicodeStringIfOriginalMatches(final AbstractUnicodeExtraField f,
                       final byte[] orig) {
  if (f != null) {
    final CRC32 crc32 = new CRC32();
    crc32.update(orig);
    final long origCRC32 = crc32.getValue();
    if (origCRC32 == f.getNameCRC32()) {
      try {
        return ZipEncodingHelper
          .UTF8_ZIP_ENCODING.decode(f.getUnicodeName());
      } catch (final IOException ex) {
        // UTF-8 unsupported?  should be impossible the
        // Unicode*ExtraField must contain some bad bytes
        // TODO log this anywhere?
        return null;
      }
    }
  }
  return null;
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

private void writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased) throws IOException {
  final boolean encodable = zipEncoding.canEncode(ze.getName());
  final ByteBuffer name = getName(ze);
  if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) {
    addUnicodeExtraFields(ze, encodable, name);
  }
  final long localHeaderStart = streamCompressor.getTotalBytesWritten();
  final byte[] localHeader = createLocalFileHeader(ze, name, encodable, phased, localHeaderStart);
  metaData.put(ze, new EntryMetaData(localHeaderStart, usesDataDescriptor(ze.getMethod(), phased)));
  entry.localDataStart = localHeaderStart + LFH_CRC_OFFSET; // At crc offset
  writeCounted(localHeader);
  entry.dataStart = streamCompressor.getTotalBytesWritten();
}

代码示例来源:origin: org.codehaus.plexus/plexus-archiver

private byte[] encodeArchiveEntry( String payload, String encoding )
  throws IOException
{
  ZipEncoding enc = ZipEncodingHelper.getZipEncoding( encoding );
  ByteBuffer encodedPayloadByteBuffer = enc.encode( payload );
  byte[] encodedPayloadBytes = new byte[encodedPayloadByteBuffer.limit()];
  encodedPayloadByteBuffer.get( encodedPayloadBytes );
  return encodedPayloadBytes;
}

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * <p>
 * Convenience method to return the entry's content as a String if isUnixSymlink()
 * returns true for it, otherwise returns null.
 * </p>
 *
 * <p>This method assumes the symbolic link's file name uses the
 * same encoding that as been specified for this ZipFile.</p>
 *
 * @param entry ZipArchiveEntry object that represents the symbolic link
 * @return entry's content as a String
 * @throws IOException problem with content's input stream
 * @since 1.5
 */
public String getUnixSymlink(final ZipArchiveEntry entry) throws IOException {
  if (entry != null && entry.isUnixSymlink()) {
    try (InputStream in = getInputStream(entry)) {
      return zipEncoding.decode(IOUtils.toByteArray(in));
    }
  }
  return null;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com