Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getRawName

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry getRawName

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry getRawName.

Prototype

public byte[] getRawName() 

Source Link

Document

Returns the raw bytes that made up the name before it has been converted using the configured or guessed encoding.

Usage

From source file:com.zimbra.cs.util.ZipUtil.java

public static String bestGuessAtEntryName(ZipArchiveEntry zae, Locale locale) {
    GeneralPurposeBit gbp = zae.getGeneralPurposeBit();
    if ((null != gbp) && gbp.usesUTF8ForNames()) {
        return (zae.getName());
    }//from  w w  w  .  j ava 2 s.c  om
    byte[] rawName = zae.getRawName();
    /* First of all, assume UTF-8.  java.util.zip assumes UTF-8 names, so this is a reasonable first
     * guess.  If the name isn't US-ASCII and it isn't UTF-8 there is a reasonable chance this
     * will fail because of the way UTF-8 works - and we can try other charsets.
     */
    String guess = convertBytesIfPossible(rawName, StandardCharsets.UTF_8);
    if (guess != null) {
        return guess;
    }
    guess = getNameFromUnicodeExtraPathIfPresent(zae);
    if (guess != null) {
        return guess;
    }
    List<String> charsetsForLocale = defaultCharsetForLocale.get(locale);
    if (null != charsetsForLocale) {
        for (String charsetForLocale : charsetsForLocale) {
            guess = convertBytesIfPossible(rawName, Charset.forName(charsetForLocale));
            if (guess != null) {
                return guess;
            }
        }
    }
    return zae.getName();
}

From source file:com.zimbra.cs.util.ZipUtil.java

/**
 * Use InfoZIP Unicode Extra Fields (if present) to set the filename
 *//* ww w.  j a va2  s . c  o  m*/
private static String getNameFromUnicodeExtraPathIfPresent(ZipArchiveEntry zae) {
    UnicodePathExtraField unicodePathExtraField = (UnicodePathExtraField) zae
            .getExtraField(UnicodePathExtraField.UPATH_ID);
    if (null == unicodePathExtraField) {
        return null;
    }
    CRC32 crc32 = new CRC32();
    crc32.update(zae.getRawName());
    long origCRC32 = crc32.getValue();

    if (origCRC32 == unicodePathExtraField.getNameCRC32()) {
        String val = convertBytesIfPossible(unicodePathExtraField.getUnicodeName(), StandardCharsets.UTF_8);
        if (null != val) {
            ZimbraLog.misc.debug("ZipUtil name '%s' from unicodeExtraPath", val);
        }
        return val;
    }
    return null;
}

From source file:org.abysm.onionzip.ZipFileExtractHelper.java

void setEncodingByDetection() throws IOException {
    UniversalDetector filenameCharsetDetector = new UniversalDetector(null);
    ZipFile zipFile = new ZipFile(zipFilename);
    try {/*w w  w  .ja va2s  .  c  o m*/
        for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile
                .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) {
            ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement();
            if (!entry.getGeneralPurposeBit().usesUTF8ForNames()) {
                byte[] filename = entry.getRawName();
                filenameCharsetDetector.handleData(filename, 0, filename.length);
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
    filenameCharsetDetector.dataEnd();
    this.encoding = filenameCharsetDetector.getDetectedCharset();
}