Tuesday, February 25, 2014

.NET Framework 4.5 Zip facility (Zip compression)

ip is one of the most accepted archive file formats. Zip format is supported in almost all operating systems with some built-in name.
  • In Windows operating system it’s implemented by the name “Compressed folders”.
  • In MAC OS it’s implemented by the name “Archive utility”.
Now in .NET we did not have built-in support for implementing Zip compression. Many developers where using third party components like “DotnetZip”. In .NET 4.5, the Zip feature is baked in the framework itself, inside the namespaceSystem.IO.Compression.
The first step is you need to reference two namespaces:
  • System.IO.Compression.FileSystem
  • System.IO.Compression
The next step is to import the below two namespaces:

Code Block :
 
using System.IO.Compression; 
Namespace zip
{
Class zip
{
ZipFile.CreateFromDirectory(@"D:\data",@"D:\data.zip");
ZipFile.ExtractToDirectory(@"D:\data.zip", @"D:\data\unzip");
}
}

Java Generics Basics

his Java generics tutorial is to introduce basic terminologies related to generics. They are demonstrated with example code and this tutorial is for beginners only.

Java Generics Example

Before we go into detail let us see an example for generics in Java. This is to recall and continue from our previous tutorial for introduction to generics. In the following example we are using the generics type implemented for collections. This shows how we can use a generic type.
package com.javapapers.java;
import java.util.ArrayList;
import java.util.List;
public class JavaGenericsExample {
  public static void main(String args[]) {
    List<String> animalsList = new ArrayList<String>();
    animalsList.add("Lion");
    animalsList.add("Crocodile");
    animalsList.add("Hyena");
    for (String animal : animalsList) {
      System.out.println(animal);
    }
  }
}

Generic Type

A generic type is the definition of a generic class. The generic type definition will have a placeholder for a type. Following are examples from Java JDK API for generic types

List – JDK Source

public interface List<E> extends Collection<E> {
...
 boolean add(E e);
...
}

ArrayList – JDK Source

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
...
}

Parameterized Type

When a generic type is instantiated, it is called parameterized type. We supply a class/type as parameter for placeholder and create a new object for a generic type. In the above java generics example, we have instantiated a List object by supplying the parameter argument as String.
List<String> animalsList = new ArrayList<String>();

Wildcard Type

Using a generic type with wildcards is wildcard type. In the below example, we have written a new method using generics which uses the wildcard type. We have not give any actual parameter as argument in the topTwo method. We have used a wildcard ? as argument. This states that we can call this method by passing a List containing any type.
package com.javapapers.java;
import java.util.ArrayList;
import java.util.List;
public class GenericsWildcardType {
  public static void main(String args[]) {
    List<String> animalsList = new ArrayList<String>();
    animalsList.add("Lion");
    animalsList.add("Crocodile");
    animalsList.add("Hyena");
    List<?> topTwoAnimals = topTwo(animalsList);
    System.out.println(topTwoAnimals);
  }
  public static List<?> topTwo(List<?> list) {
    return list.subList(0, 2);
  }
}

Bounded Wildcard Type

We can restrict the wildcard type arguments as applicable to only a specific subtype or supertype. Instead of any type at the place of the wildcard ?, it is restricted to the sub or super classes of a specific class. For example, in the above wildcard type example we can add the keyword ‘extends’ as below and restrict it to a subtype only. This is known as upper bound. Similarly ‘super’ can be used to restrict it to a supertype and is known as lower bound.
public static List<?> topTwo(List<? extends String> list) {
  return list.subList(0, 2);
}

Generics Custom Type

Following is an example for Java generics demonstrating generic type, parameterized type, wildcard type and bounded wildcard type.
package com.javapapers.java;
/*
 *OUTPUT:
 Java Generics Rocks: Tea
 Java Generics Poured: Tea
 */
public class CustomGenericType {
  public static void main(String args[]) {
    Cup<Drink> cup = new Cup<Drink>();
    Cup<Tea> cupOfTea = new Cup<Tea>(new Tea());
    System.out.println("Java Generics Rocks: " + cupOfTea.pour());
    cup.fillFrom(cupOfTea);
    System.out.println("Java Generics Poured: " + cup.pour());
  }
}
class Drink {
  @Override
  public String toString() {
    return "empty";
  }
}
class Tea extends Drink {
  @Override
  public String toString() {
    return "Tea";
  }
}
class Cup<E> {
  String content;
  public Cup() {
  }
  public Cup(E e) {
    content = e.toString();
  }
  public void fillFrom(Cup<? extends Drink> c) {
    content = c.pour();
  }
  public String pour() {
    return content;
  }
}
This Java generics tutorial you have read now is part two of Generics series and there is lot more to come, keep watching.