ZIP, GZIP Streams in java
- 1.
Язык Java
Работа с архивами
java.util.zip
java.util.jar
- 2.
GZipInputStream
class GZIPInputStream extends InflaterInputStream
public GZIPInputStream(InputStream in, int size) throws IOException
public GZIPInputStream(InputStream in) throws IOException
public int read(byte[] buf, int off, int len) throws IOException
public void close() throws IOException
- 3.
GZipOutputStream
class GZIPOutputStream extends DeflaterOutputStream
public GZIPOutputStream(OutputStream out, int size)
public GZIPOutputStream(OutputStream out) throws IOException
public synchronized void write(byte[] buf, int off, int len) throws IOException
public void finish() throws IOException
- 4.
Пример использования (упаковка)
FileInputStream fi=null; try{ try{ fi=new FileInputStream("in.txt"); go=new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream("out.gz"))); int l=fi.available(); byte[] b=new byte[l]; fi.read(b); go.write(b); } finally{ if(go!=null) go.close(); if(fi!=null) fi.close(); } }catch(Exception e) { System.out.println(e); }
- 5.
Пример использования (распаковка)
BufferedOutputStream bo = null; try { try { gi = new GZIPInputStream(new BufferedInputStream(new FileInputStream("out.gz"))); int i=0; bo = new BufferedOutputStream(new FileOutputStream("out2.txt")); while((i=gi.read())!=-1) bo.write(i); } finally { if (gi != null) gi.close(); if (bo != null) bo.close(); } } catch (Exception e) { System.out.println(e); }
- 6.
ZipInputStream
class ZipInputStream extends InflaterInputStream implements ZipConstants
public ZipInputStream(InputStream in)
public ZipEntry getNextEntry() throws IOException
public void closeEntry() throws IOException
public int available() throws IOException
public int read(byte[] b, int off, int len) throws IOException
public long skip(long n) throws IOException
public void close() throws IOException
- 7.
ZipOutputStream
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants
public ZipOutputStream(OutputStream out)
public void setComment(String comment)
public void setMethod(int method)
public void setLevel(int level)
public void putNextEntry(ZipEntry e) throws IOException
public void closeEntry() throws IOException
public synchronized void write(byte[] b, int off, int len)
public void finish() throws IOException
public void close() throws IOException
- 8.
ZipEntry
class ZipEntry implements ZipConstants, Cloneable
public ZipEntry(String name)
public ZipEntry(ZipEntry e)
public String getName()
public void setTime(long time)
public long getTime()
public void setSize(long size)
public long getSize()
public long getCompressedSize()
public void setCompressedSize(long csize)
public void setMethod(int method)
public int getMethod()
public void setExtra(byte[] extra)
public byte[] getExtra()
public void setComment(String comment)
public String getComment()
- 9.
Пример использования
public void createZip() { try { try { zo = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(s+".zip"))); File f=new File(s); String s1=f.getPath(); addFolderToZip(zo,f.getName(),f); } finally { if (zo != null) { zo.close(); } } } catch (Exception e) { System.out.println(e); } }
- 10.
Пример использования (продолжение)
public void addFolderToZip(ZipOutputStream zo, String path, File dir) { File[] fs = dir.listFiles(); for (File f : fs) { if (f.isFile()) { addFile(zo, path, f); } else { addFolderToZip(zo, path+""+f.getName(), f); } } }
- 11.
Пример использования (продолжение)
public void addFile(ZipOutputStream zo, String path, File f) { ZipEntry ze = new ZipEntry(path+""+f.getName()); BufferedInputStream bi = null; try { try { zo.putNextEntry(ze); bi = new BufferedInputStream(new FileInputStream(f.getPath())); byte[] b = new byte[1024]; int i = 0; while ((i = bi.read(b)) != -1) zo.write(b, 0, i); } finally { zo.closeEntry(); if (bi != null) bi.close(); } } catch (IOException e) { System.out.println(e); } }
- 12.
Конец
Вопросы
e-mail: a.bovanenko@gmail.com