ru.protek.component.xzip
Class XZipInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.util.zip.InflaterInputStream
              extended by ru.protek.component.xzip.XZipInputStream
All Implemented Interfaces:
java.io.Closeable, XZipConstants

public class XZipInputStream
extends java.util.zip.InflaterInputStream
implements XZipConstants

Это моя переделка класса java.util.zip.ZipInputStream. В стандартный класс добавлена возможность раззиповывать архивы, зашифрованные паролем. Алгоритм шифрации для старого зипа (версии 2). Все, что добавлено нового, находиться в конце файла (кое-какие изменения есть и в теле класса, но это не важно). Пример использования (из криптованного паролем "vlad" архива "crypt.zip" извлекаются все файлы):

  XZipEntry ze;
  FileOutputStream fo;
  FileInputStream fi = new FileInputStream("crypt.zip");
  XZipInputStream zi = new XZipInputStream(fi);
  zi.setPassword("vlad");
  zi.setPasswordEncode("Cp1256");
  for(ze = zi.getNextEntry(); ze != null; ze = zi.getNextEntry()) {
      fo = new FileOutputStream(ze.getName() + ".decrypt");
      for(int r = zi.read();r >= 0; r = zi.read()) fo.write(r);
      fo.close();
      zi.closeEntry();
  }
  zi.close();
 


Field Summary
protected  XCRC32 crc32
          Значение хэш функции CRC32.
protected  byte[] originalbuf
           
protected  byte[] randomBeginBytes
          Набор случайных чисел, которые шифруются перед шифруемым сообщением.
 
Fields inherited from class java.util.zip.InflaterInputStream
buf, inf, len
 
Fields inherited from class java.io.FilterInputStream
in
 
Fields inherited from interface ru.protek.component.xzip.XZipConstants
CENATT, CENATX, CENCOM, CENCRC, CENDSK, CENEXT, CENFLG, CENHDR, CENHOW, CENLEN, CENNAM, CENOFF, CENSIG, CENSIZ, CENTIM, CENVEM, CENVER, ENDCOM, ENDHDR, ENDOFF, ENDSIG, ENDSIZ, ENDSUB, ENDTOT, EXTCRC, EXTHDR, EXTLEN, EXTSIG, EXTSIZ, LOCCRC, LOCEXT, LOCFLG, LOCHDR, LOCHOW, LOCLEN, LOCNAM, LOCSIG, LOCSIZ, LOCTIM, LOCVER
 
Constructor Summary
XZipInputStream(java.io.InputStream theIn)
          Creates a new ZIP input stream.
 
Method Summary
 int available()
          Returns 0 after EOF has reached for the current entry data, otherwise always return 1.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 void closeEntry()
          Closes the current ZIP entry and positions the stream for reading the next entry.
protected  XZipEntry createZipEntry(java.lang.String name)
          Creates a new XZipEntry object for the specified entry name.
protected  void fill()
          Переписанный вертуальный метод, в котором, собственно, происходит расшифровка буфера, перед тем как распокавать этот буфер.
 XZipEntry getNextEntry()
          Переписанная функция getNextEntry() выдает очередное вхождение зипованного файла в архив.
 XZipEntry getNextEntry1()
          Reads the next ZIP file entry and positions the stream at the beginning of the entry data.
 java.lang.String getPassword()
          Возврашает тикущей пароль.
 java.lang.String getPasswordEncode()
          Возврашает кодировку в которой установлен пароль.
 int read(byte[] b, int off, int len)
          Reads from the current ZIP entry into an array of bytes.
 void setPassword(java.lang.String thePassword)
          Устанавливает пароль.
 void setPasswordEncode(java.lang.String thePasswordEncode)
          Устанавливает кодировку строки пароля.
 long skip(long n)
          Skips specified number of bytes in the current ZIP entry.
 
Methods inherited from class java.util.zip.InflaterInputStream
mark, markSupported, read, reset
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

originalbuf

protected byte[] originalbuf

randomBeginBytes

protected byte[] randomBeginBytes
Набор случайных чисел, которые шифруются перед шифруемым сообщением. Необходим для предотвращения попыток вскрытия методом "plaintext attack".


crc32

protected XCRC32 crc32
Значение хэш функции CRC32. Необходима для шифрации и получения ключевой информации.

Constructor Detail

XZipInputStream

public XZipInputStream(java.io.InputStream theIn)
Creates a new ZIP input stream.

Parameters:
theIn - the actual input stream
Method Detail

getNextEntry1

public XZipEntry getNextEntry1()
                        throws java.io.IOException
Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Returns:
the next ZIP file entry, or null if there are no more entries
Throws:
java.util.zip.ZipException - if a ZIP file error has occurred
java.io.IOException - if an I/O error has occurred

closeEntry

public void closeEntry()
                throws java.io.IOException
Closes the current ZIP entry and positions the stream for reading the next entry.

Throws:
java.util.zip.ZipException - if a ZIP file error has occurred
java.io.IOException - if an I/O error has occurred

available

public int available()
              throws java.io.IOException
Returns 0 after EOF has reached for the current entry data, otherwise always return 1.

Programs should not count on this method to return the actual number of bytes that could be read without blocking.

Overrides:
available in class java.util.zip.InflaterInputStream
Returns:
1 before EOF and 0 after EOF has reached for current entry.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads from the current ZIP entry into an array of bytes. Blocks until some input is available.

Overrides:
read in class java.util.zip.InflaterInputStream
Parameters:
b - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the actual number of bytes read, or -1 if the end of the entry is reached
Throws:
java.util.zip.ZipException - if a ZIP file error has occurred
java.io.IOException - if an I/O error has occurred

skip

public long skip(long n)
          throws java.io.IOException
Skips specified number of bytes in the current ZIP entry.

Overrides:
skip in class java.util.zip.InflaterInputStream
Parameters:
n - the number of bytes to skip
Returns:
the actual number of bytes skipped
Throws:
java.util.zip.ZipException - if a ZIP file error has occurred
java.io.IOException - if an I/O error has occurred
java.lang.IllegalArgumentException - if n < 0

close

public void close()
           throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.util.zip.InflaterInputStream
Throws:
java.io.IOException - if an I/O error has occurred

createZipEntry

protected XZipEntry createZipEntry(java.lang.String name)
Creates a new XZipEntry object for the specified entry name.

Parameters:
name - the ZIP file entry name
Returns:
the XZipEntry just created

getNextEntry

public XZipEntry getNextEntry()
                       throws java.io.IOException
Переписанная функция getNextEntry() выдает очередное вхождение зипованного файла в архив.

Returns:
объект XZipEntry - очередное вхождение зипованного файла в архив.
Throws:
java.io.IOException - ошибка ввода-вывода.

fill

protected void fill()
             throws java.io.IOException
Переписанный вертуальный метод, в котором, собственно, происходит расшифровка буфера, перед тем как распокавать этот буфер.

Overrides:
fill in class java.util.zip.InflaterInputStream
Throws:
java.io.IOException - ошибка ввода-вывода.

getPassword

public java.lang.String getPassword()
Возврашает тикущей пароль.

Returns:
Пароль.

setPassword

public void setPassword(java.lang.String thePassword)
Устанавливает пароль.

Parameters:
thePassword - Пароль.

getPasswordEncode

public java.lang.String getPasswordEncode()
Возврашает кодировку в которой установлен пароль.

Returns:
кодировка строки пароля.

setPasswordEncode

public void setPasswordEncode(java.lang.String thePasswordEncode)
Устанавливает кодировку строки пароля.

Parameters:
thePasswordEncode - кодировка строки пароля.