ZIP, GZIP Streams in java


Published on

It's about gzip and zip streams in java se.

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

  • ZIP, GZIP Streams in java

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