All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zving.preloader.zip.AbstractUnicodeExtraField Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package com.zving.preloader.zip;

import java.io.UnsupportedEncodingException;
import java.util.zip.CRC32;
import java.util.zip.ZipException;

public abstract class AbstractUnicodeExtraField
  implements ZipExtraField
{
  private long nameCRC32;
  private byte[] unicodeName;
  private byte[] data;
  
  protected AbstractUnicodeExtraField() {}
  
  protected AbstractUnicodeExtraField(String text, byte[] bytes, int off, int len)
  {
    CRC32 crc32 = new CRC32();
    crc32.update(bytes, off, len);
    this.nameCRC32 = crc32.getValue();
    try
    {
      this.unicodeName = text.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
      throw new RuntimeException("FATAL: UTF-8 encoding not supported.", e);
    }
  }
  
  protected AbstractUnicodeExtraField(String text, byte[] bytes)
  {
    this(text, bytes, 0, bytes.length);
  }
  
  private void assembleData()
  {
    if (this.unicodeName == null) {
      return;
    }
    this.data = new byte[5 + this.unicodeName.length];
    
    this.data[0] = 1;
    System.arraycopy(ZipLong.getBytes(this.nameCRC32), 0, this.data, 1, 4);
    System.arraycopy(this.unicodeName, 0, this.data, 5, this.unicodeName.length);
  }
  
  public long getNameCRC32()
  {
    return this.nameCRC32;
  }
  
  public void setNameCRC32(long nameCRC32)
  {
    this.nameCRC32 = nameCRC32;
    this.data = null;
  }
  
  public byte[] getUnicodeName()
  {
    return this.unicodeName;
  }
  
  public void setUnicodeName(byte[] unicodeName)
  {
    this.unicodeName = unicodeName;
    this.data = null;
  }
  
  public byte[] getCentralDirectoryData()
  {
    if (this.data == null) {
      assembleData();
    }
    return this.data;
  }
  
  public ZipShort getCentralDirectoryLength()
  {
    if (this.data == null) {
      assembleData();
    }
    return new ZipShort(this.data.length);
  }
  
  public byte[] getLocalFileDataData()
  {
    return getCentralDirectoryData();
  }
  
  public ZipShort getLocalFileDataLength()
  {
    return getCentralDirectoryLength();
  }
  
  public void parseFromLocalFileData(byte[] buffer, int offset, int length)
    throws ZipException
  {
    if (length < 5) {
      throw new ZipException("UniCode path extra data must have at least 5 bytes.");
    }
    int version = buffer[offset];
    if (version != 1) {
      throw new ZipException("Unsupported version [" + version + "] for UniCode path extra data.");
    }
    this.nameCRC32 = ZipLong.getValue(buffer, offset + 1);
    this.unicodeName = new byte[length - 5];
    System.arraycopy(buffer, offset + 5, this.unicodeName, 0, length - 5);
    this.data = null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy