Java Tutorial/File/ZipInputStream

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create ZipInputStream from FileInputStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class MainClass {
  public static void main(String[] args) throws IOException {
    for (int i = 0; i < args.length; i++) {
      FileInputStream fin = new FileInputStream(args[i]);
      ZipInputStream zin = new ZipInputStream(fin);
      ZipEntry ze = null;
      while ((ze = zin.getNextEntry()) != null) {
        System.out.println("Unzipping " + ze.getName());
        FileOutputStream fout = new FileOutputStream(ze.getName());
        for (int c = zin.read(); c != -1; c = zin.read()) {
          fout.write(c);
        }
        zin.closeEntry();
        fout.close();
      }
      zin.close();
    }
  }
}





Decompress a zip file using ZipInputStream

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
      System.out.println("Unzipping: " + entry.getName());
      int size;
      byte[] buffer = new byte[2048];
      FileOutputStream fos = new FileOutputStream(entry.getName());
      BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
      while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
        bos.write(buffer, 0, size);
      }
      bos.flush();
      bos.close();
    }
    zis.close();
    fis.close();
  }
}





Read zip file with ZipInputStream

import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
class ZipReader {
  public static void main(String[] args) throws Exception {
    ZipInputStream zis = null;
    FileInputStream fis = new FileInputStream(args[0]);
    zis = new ZipInputStream(fis);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null)
      System.out.println(ze.getName());
  }
}





Unzip using the ZipInputStream

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class MainClass {
  public static void main(String[] args) throws IOException {
    for (int i = 0; i < args.length; i++) {
      FileInputStream fin = new FileInputStream(args[i]);
      ZipInputStream zin = new ZipInputStream(fin);
      ZipEntry ze = null;
      while ((ze = zin.getNextEntry()) != null) {
        System.out.println("Unzipping " + ze.getName());
        FileOutputStream fout = new FileOutputStream(ze.getName());
        for (int c = zin.read(); c != -1; c = zin.read()) {
          fout.write(c);
        }
        zin.closeEntry();
        fout.close();
      }
      zin.close();
    }
  }
}