SlideShare a Scribd company logo

      
       Язык Java 
      
     
      
       Работа с архивами 
       java.util.zip 
       java.util.jar

      
       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

      
       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

      
       Пример использования (упаковка) 
      
     
      
       
        
         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);   }

      
       Пример использования (распаковка)  
      
     
      
       
        
         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);   }

      
       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

      
       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

      
       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()

      
       Пример использования 
      
     
      
       
        
         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);   }   }

      
       Пример использования (продолжение) 
      
     
      
       
        
         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);   }   }   }

      
       Пример использования (продолжение) 
      
     
      
       
        
         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);   }   }

      
       Конец 
      
     
      
       Вопросы 
       e-mail: a.bovanenko@gmail.com

More Related Content

PDF
File. Java
PDF
Go serving: Building server app with go
PDF
Infinum android talks_10_getting groovy on android
PPTX
GopherCon Denver LT 2018
PDF
Coding in GO - GDG SL - NSBM
PDF
Swift 4 : Codable
PDF
Laying Pipe with Transmogrifier
PPT
Oop lecture9 11
File. Java
Go serving: Building server app with go
Infinum android talks_10_getting groovy on android
GopherCon Denver LT 2018
Coding in GO - GDG SL - NSBM
Swift 4 : Codable
Laying Pipe with Transmogrifier
Oop lecture9 11

What's hot (16)

PPTX
Golang iran - tutorial go programming language - Preliminary
PDF
Program to-copy-contents-of-one-file-to-another
PDF
Workshop programs
ODP
Migrations With Transmogrifier
PDF
Geeks Anonymes - Le langage Go
PDF
Closures for Java
PDF
Go破壊
PPTX
Making Java Groovy (JavaOne 2013)
PDF
Migrating from drupal to plone with transmogrifier
PPTX
MessagePack - An efficient binary serialization format
PDF
Groovy Update, Groovy Ecosystem, and Gaelyk -- Devoxx 2010 -- Guillaume Laforge
PDF
C++ prgms io file unit 7
ODP
Clojure made really really simple
PDF
File input output in Java
PDF
KISS - im Prinzip ganz einfach
DOCX
java copy file program
Golang iran - tutorial go programming language - Preliminary
Program to-copy-contents-of-one-file-to-another
Workshop programs
Migrations With Transmogrifier
Geeks Anonymes - Le langage Go
Closures for Java
Go破壊
Making Java Groovy (JavaOne 2013)
Migrating from drupal to plone with transmogrifier
MessagePack - An efficient binary serialization format
Groovy Update, Groovy Ecosystem, and Gaelyk -- Devoxx 2010 -- Guillaume Laforge
C++ prgms io file unit 7
Clojure made really really simple
File input output in Java
KISS - im Prinzip ganz einfach
java copy file program
Ad

Viewers also liked (7)

PPTX
Индексный поиск по регулярным выражениям (Александр Коротков)
PPT
sphinx Hlpp2008
ODP
Regular Expressions
ODP
Regex Presentation
PDF
Python. re
PDF
Python sqlite3
PPT
Practical Example of grep command in unix
Индексный поиск по регулярным выражениям (Александр Коротков)
sphinx Hlpp2008
Regular Expressions
Regex Presentation
Python. re
Python sqlite3
Practical Example of grep command in unix
Ad

Similar to ZIP, GZIP Streams in java (6)

ODP
Java IO. Streams
DOCX
file compression/zip file.report
PDF
5. Ввод-вывод, доступ к файловой системе
PDF
AVTOKYO2013.5 Detail of CVE-2013-4787 (Master Key Vulnerability)
PDF
Java 7 at SoftShake 2011
PDF
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
Java IO. Streams
file compression/zip file.report
5. Ввод-вывод, доступ к файловой системе
AVTOKYO2013.5 Detail of CVE-2013-4787 (Master Key Vulnerability)
Java 7 at SoftShake 2011
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin

More from Alexey Bovanenko (20)

PDF
python dict
PDF
Python. Строки
PDF
Python. Введение
PPT
PPT
Конвертация строковых данных в числовые
ODP
PPT
Обработка символов в языке C
ODP
PPTX
Javascript functions
PPTX
Javascript String object
PDF
Конструктор копирования
PDF
Tempale Intro
PDF
transaction. php
PDF
cookie. support by php
PDF
php sessions
ODP
Classes: Number, String, StringBuffer, StringBuilder
ODP
Объект Logger
ODP
Исключительные ситуации
ODP
Drag And Drop Windows Forms
PDF
enum. Java.
python dict
Python. Строки
Python. Введение
Конвертация строковых данных в числовые
Обработка символов в языке C
Javascript functions
Javascript String object
Конструктор копирования
Tempale Intro
transaction. php
cookie. support by php
php sessions
Classes: Number, String, StringBuffer, StringBuilder
Объект Logger
Исключительные ситуации
Drag And Drop Windows Forms
enum. Java.

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Complications of Minimal Access Surgery at WLH
PDF
Trump Administration's workforce development strategy
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Updated Idioms and Phrasal Verbs in English subject
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
Supply Chain Operations Speaking Notes -ICLT Program
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
UNIT III MENTAL HEALTH NURSING ASSESSMENT
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Complications of Minimal Access Surgery at WLH
Trump Administration's workforce development strategy
Anesthesia in Laparoscopic Surgery in India
Orientation - ARALprogram of Deped to the Parents.pptx
Updated Idioms and Phrasal Verbs in English subject
Final Presentation General Medicine 03-08-2024.pptx
Paper A Mock Exam 9_ Attempt review.pdf.

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