Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы DemoZip.java

Оглавление

Назад

// ==========================================
// DemoZip.java
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.io.*;
import java.util.*;
import java.util.zip.*;

public class DemoZip 
{
  public static void main(String args[])
  {
    String s;
    int i;
    
    System.out.println(
      "Enter full path: ");
	
    s = new String(getKbdString());
      
    File f = new File(s);
      
    if(!f.exists())
    {
      System.out.println("\nNot found: " + s);
      System.exit(0);
    }
      
    if(!f.isDirectory())
    {
      System.out.println(
	"\nNot directory: " + s);
      System.exit(0);
    }
      
    ZipOutputStream zos = 
      createZipOutputStream("!temp.zip");
      
    String[] sDirList = f.list();
      
    try
    {    
      for(i = 0; i < sDirList.length; i++)
      {
	File f1 = new File(
	  s + File.separator + sDirList[i]);
	
	if(f1.isFile())
	{
	  addFileToZip(zos, s + File.separator,
	     sDirList[i]);
	}
      }  
      zos.close();
    }	
    catch(Exception ex)
    {
      System.out.println(ex.toString());
    }
  }  
  
  // ============================================
  // createZipOutputStream
  // ============================================
  static ZipOutputStream 
    createZipOutputStream(String szPath)
  {
    File tempfile;  
    ZipOutputStream zos = null;
    
    try
    {    
      tempfile = new File(szPath);
      
      zos = new ZipOutputStream(
        new FileOutputStream(tempfile));
	
      zos.setLevel(
	Deflater.DEFAULT_COMPRESSION);
    }   
    catch(Exception ex)
    {
      System.out.println(ex.toString());
    }
    return zos;
  }
  
  // ============================================
  // addFileToZip
  // ============================================
  static void addFileToZip(ZipOutputStream zos, 
    String szPath, String szName) 
    throws Exception
  {
    System.out.print(szPath + szName);
      
    ZipEntry ze;
    ze = new ZipEntry(szName);
    zos.putNextEntry(ze);
      
    FileInputStream fis = 
      new FileInputStream(szPath + szName);
  
    byte[] buf = new byte[8000];
    int nLength;
    while(true)
    {
      nLength = fis.read(buf);
      if(nLength < 0)
	  break;
      zos.write(buf, 0, nLength);
    }
      
    fis.close();
    zos.closeEntry();
    
    long nSize = ze.getSize();
    long nCompressedSize = 
      ze.getCompressedSize();
    
    long nPercent = 100 - 
      ((nCompressedSize * 100) / nSize);
    
    System.out.println(" " + nSize + " (" +
      nCompressedSize + ") " + nPercent + "%");  
  }

  // ============================================
  // getKbdString
  // ============================================
  static public String getKbdString()
  {
    byte bKbd[] = new byte[256];
    int iCnt = 0;
    String szStr = "";
    
    try
    {
      iCnt = System.in.read(bKbd);
    }
    catch(Exception ex)
    {
      System.out.println(ex.toString()); 
    }
      
    szStr = new String(bKbd, 0, iCnt);
    szStr = szStr.trim();
    return szStr;
  }
}

[Назад]