Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Creating a ZIP file in Java

This section teaches you about the ZIP file with java program.


How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value

Creating a ZIP file in Java


Zip File: Zip file format is the popular method of data compression. Zip file contains many files in compressed format. You can say the files are stored in the zip format with compression. The file which is in the zip format holds the ".zip" extension.

You can make executable file in zip format. You can archive data from the zip file by extracting it. For create a zip file from other type of file, following program gives you the best way of making it possible through you code in java application.

Program Result:

This program makes a zip file from only one file which is given by you. It takes a file to make it into zip format and it takes the zip file name which has to be created from the given file. Finally, it makes the zip file. You can archive the file from the zip file by extracting, if you want.

Code Description:

Several methods and APIs are used in the following program for making a zip file from the given file. These are explained as follows:

ZipOutputStream is the class of*; package. This class is used to compression the file whether the file is compressed on uncompressed.

Above mentioned is the setLevel() method of ZipOutputStream class which sets the level of the compression process. This method takes a argument which is the built-in field of the Deflater class.

This is the class of*; package. This class provides the compression using the ZLIB compression library.

Here is the code of the program:


public class ZipCreateExample{
  public static void main(String[] argsthrows IOException{
  System.out.print("Please enter file name to zip : ");
  BufferedReader input = new BufferedReader
new InputStreamReader(;
  String filesToZip = input.readLine();
  File f = new File(filesToZip);
  System.out.println("File not found.");
  System.out.print("Please enter zip file name : ");
  String zipFileName = input.readLine();
  if (!zipFileName.endsWith(".zip"))
  zipFileName = zipFileName + ".zip";
  byte[] buffer = new byte[18024];
  ZipOutputStream out = new ZipOutputStream
new FileOutputStream(zipFileName));
  FileInputStream in = new FileInputStream(filesToZip);
  out.putNextEntry(new ZipEntry(filesToZip));
  int len;
  while ((len = 0){
  out.write(buffer, 0, len);
  catch (IllegalArgumentException iae){
  catch (FileNotFoundException fnfe){
  catch (IOException ioe){

Download this example.


Share on Google+Share on Google+

Creating a ZIP file in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Creating a ZIP file in Java  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 19, 2011

package com.Tutorial; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Random; import android.content.Context; import; import; import; import; import; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * Custom SurfaceView to handle everything we need from physics to drawings. * * @author martin */ public class Panel extends SurfaceView implements SurfaceHolder.Callback { /** * Thread which contains our game loop. */ private RockScissorsPaperThread _thread; /** * List of graphics we already have to handle. */ private ArrayList<Graphic> _graphics = new ArrayList<Graphic>(); /** * List of explosions we have to draw. */ private ArrayList<Graphic> _explosions = new ArrayList<Graphic>(); /** * The graphic we have while our finger touches the screen. */ private Graphic _currentGraphic; /** * Last coordinates to store the point we first touched the screen. */ private Graphic.Coordinates _lastCoords; /** * Sound pool which allows us to play multiple sounds at once. */ private SoundPool _soundPool; /** * Id of our explosion sound. */ private int _playbackFile = 0; /** * Cache variable for all used images. */ private Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>(); /** * Constructor called on instantiation. * @param context Context of calling activity. */ public Panel(Context context) { super(context); fillBitmapCache(); _soundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 100); _playbackFile = _soundPool.load(getContext(), R.raw.explosion, 0); getHolder().addCallback(this); _thread = new RockScissorsPaperThread(this); setFocusable(true); } /** * Fill the bitmap cache. */ private void fillBitmapCache() { _bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon)); _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt)); _bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper)); _bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(), R.drawable.scissors)); _bitmapCache.put(R.drawable.paper, BitmapFactory.decodeResource(getResources(), R.drawable.paper)); _bitmapCache.put(R.drawable.rock, BitmapFactory.decodeResource(getResources(), R.drawable.rock)); _bitmapCache.put(R.drawable.smaller, BitmapFactory.decodeResource(getResources(), R.drawable.smaller)); _bitmapCache.put(R.drawable.small, BitmapFactory.decodeResource(getResources(), R.drawable.small)); _bitmapCache.put(R.drawable.big, BitmapFactory.decodeResource(getResources(), R.drawable.big)); } /** * Process the MotionEvent. */ @Override public boolean onTouchEvent(MotionEvent event) { synchronized (getHolder()) { Graphic graphic = null; if (event.getAction() == MotionEvent.ACTION_DOWN) { Random random = new Random(); int rand = Math.abs(random.nextInt() % 3); switch (rand) { case 0: graphic = new Graphic(_bitmapCache.get(R.drawable.scissors)); graphic.setType("scissors"); break; case 1: graphic = new Graphic(_bitmapCache.get(R.drawable.rock)); graphic.setType("rock"); break; case 2: graphic = new Graphic(_bitmapCache.get(R.drawable.paper)); graphic.setType("paper"); break; default: throw new RuntimeException("RANDOM not between 0 and 2: " + rand); } graphic.getCoordinates().setTouchedX((int) event.getX()); graphic.getCoordinates().setTouchedY((int) event.getY()); _lastCoords = new Graphic(_bitmapCache.get(R.drawable.icon)).getCoordinates(); _lastCoords.setTouchedX(graphic.getCoordinates().getTouchedX()); _lastCoords.setTouchedY(graphic.getCoordinates().getTouchedY()); _currentGraphic = graphic; } else if (event.getAction() == MotionEvent.ACTION_MOVE) { _currentGraphic.getCoordinates().setTouchedX((int) event.getX()); _currentGraphic.getCoordinates().setTouchedY((int) event.getY()); } else if (event.getAction() == MotionEvent.ACTION_UP) { // calculating speed calculatingSpeedX((int) event.getX()); calculatingSpeedY((int) event.getY()); _graphics.add(_currentGraphic); _currentGraphic = null; } return true; } } /** * Calculating the speed of the item. * Using the difference between start point and release point with smoothing factor. * * @param currentX X coordinate which represent the last point. */ private void calculatingSpeedX(int currentX) { if (currentX != _lastCoords.getTouchedX()) { int diff = currentX - _lastCoords.getTouchedX(); int amplitude = diff / 10; _currentGraphic.getSpeed().setX(amplitude); } else { _currentGraphic.getSpeed().setX(0); } } /** * Calculating the speed of the item. * Using the difference between start point and release point with smoothing factor. * * @param currentY Y coordinate which represent the last point. */ private void calculatingSpeedY(int currentY) { if (currentY != _lastCoords.getTouchedY()) { int diff = currentY - _lastCoords.getTouchedY(); int amplitude = diff / 10; _currentGraphic.getSpeed().setY(amplitude); } else { _currentGraphic.getSpeed().setY(0); } } /** * Update the physics of each item already added to the panel. * Not including items which are currently exploding and moved by a touch event. */ public void updatePhysics() { Graphic.Coordinates coord; Graphic.Speed speed; for (Graphic graphic : _graphics) { coord = graphic.getCoordinates(); speed = graphic.getSpeed(); // Direction coord.setX(coord.getX() + speed.getX()); coord.setY(coord.getY() + speed.getY()); // borders for x... if (coord.getX() < 0) { speed.setX(-speed.getX()); coord.setX(-coord.getX()); } else if (coord.getX() + graphic.getBitmap().getWidth() > getWidth()) { speed.setX(-speed.getX()); coord.setX(coord.getX() + getWidth() - (coord.getX() + graphic.getBitmap().getWidth())); } // borders for y... if (coord.getY() < 0) { speed.setY(-speed.getY()); coord.setY(-coord.getY()); } else if (coord.getY() + graphic.getBitmap().getHeight() > getHeight()) { speed.setY(-speed.getY()); coord.setY(coord.getY() + getHeight() - (coord.getY() + graphic.getBitmap().getHeight())); } } } /** * Check all items on the panel for collisions and find the winner. * The loser will added to the list of explosions. */ public void checkForWinners() { ArrayList<Graphic> toExplosion = new ArrayList<Graphic>(); for (Graphic grapics : _graphics) { for (Graphic battleGraphic : _graphics) { if (battleGraphic != grapics && !(toExplosion.contains(battleGraphic) || toExplosion.contains(grapics))) { if (!battleGraphic.getType().equals(grapics.getType()) && checkCollision(battleGraphic, grapics)) { if (firstWins(battleGraphic.getType(), grapics.getType())) { toExplosion.add(grapics);, 1, 1, 0, 0, 1); } } } else { continue; } } } if (!toExplosion.isEmpty()) { _explosions.addAll(toExplosion); _graphics.removeAll(toExplosion); } } /** * Mathematical calculation of a collision between two items. * * @param first First item. * @param second Second item. * @return Returns true if first and second item hit each other. */ private boolean checkCollision(Graphic first, Graphic second) { boolean retValue = false; int width = first.getBitmap().getWidth(); int height = first.getBitmap().getHeight(); int firstXRangeStart = first.getCoordinates().getX(); int firstXRangeEnd = firstXRangeStart + width; int firstYRangeStart = first.getCoordinates().getY(); int firstYRangeEnd = firstYRangeStart + height; int secondXRangeStart = second.getCoordinates().getX(); int secondXRangeEnd = secondXRangeStart + width; int secondYRangeStart = second.getCoordinates().getY(); int secondYRangeEnd = secondYRangeStart + height; if ((secondXRangeStart >= firstXRangeStart && secondXRangeStart <= firstXRangeEnd) || (secondXRangeEnd >= firstXRangeStart && secondXRangeEnd <= firstXRangeEnd)) { if ((secondYRangeStart >= firstYRangeStart && secondYRangeStart <= firstYRangeEnd) || (secondYRangeEnd >= firstYRangeStart && secondYRangeEnd <= firstYRangeEnd)) { retValue = true; } } return retValue; } /** * True if first type wins, false if second type wins. * * @param firstType Type of the first object. * @param secondType Type of the second object. * @return Returns who wins. */ private boolean firstWins(String firstType, String secondType) { if (firstType.equals("explosion") || secondType.equals("explosion")) { return false; } else if (firstType.equals("scissors") && secondType.equals("paper")) { return true; } else if (firstType.equals("scissors") && secondType.equals("rock")) { return false; } else if (firstType.equals("paper") && secondType.equals("scissors")) { return false; } else if (firstType.equals("paper") && secondType.equals("rock")) { return true; } else if (firstType.equals("rock") && secondType.equals("paper")) { return false; } else if (firstType.equals("rock") && secondType.equals("scissors")) { return true; } else { throw new RuntimeException("Fight not possible!"); } } /** * Draw on the SurfaceView. * Order: * <ul> * <li>Background image</li> * <li>Items on the panel</li> * <li>Explosions</li> * <li>Item moved by hand</li> * </ul> */ @Override public void onDraw(Canvas canvas) { // draw the background canvas.drawBitmap(_bitmapCache.get(R.drawable.abstrakt), 0, 0, null); Bitmap bitmap; Graphic.Coordinates coords; // draw the normal items for (Graphic graphic : _graphics) { bitmap = graphic.getBitmap(); coords = graphic.getCoordinates(); canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null); } // draw the explosions ArrayList<Graphic> finishedExplosion = new ArrayList<Graphic>(); for (Graphic graphic : _explosions) { if (!graphic.getType().equals("explosion")) { graphic.setType("explosion"); graphic.setExplosionStep(0); graphic.getSpeed().setX(0); graphic.getSpeed().setY(0); graphic.setBitmap(_bitmapCache.get(R.drawable.smaller)); bitmap = graphic.getBitmap(); coords = graphic.getCoordinates(); canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null); } else { switch (graphic.getExplosionStep()) { case 10: bitmap = _bitmapCache.get(R.drawable.small); graphic.setBitmap(bitmap); break; case 20: bitmap = _bitmapCache.get(R.drawable.big); graphic.setBitmap(bitmap); break; case 30: bitmap = _bitmapCache.get(R.drawable.small); graphic.setBitmap(bitmap); break; case 40: bitmap = _bitmapCache.get(R.drawable.smaller); graphic.setBitmap(bitmap); break; default: bitmap = graphic.getBitmap(); } coords = graphic.getCoordinates(); canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null); graphic.setExplosionStep(graphic.getExplosionStep() + 1); } if (graphic.getExplosionStep() > 50) { finishedExplosion.add(graphic); } } // remove all Objects who are already fully exploded... if (!finishedExplosion.isEmpty()) { _explosions.removeAll(finishedExplosion); } // draw current graphic at last... if (_currentGraphic != null) { bitmap = _currentGraphic.getBitmap(); coords = _currentGraphic.getCoordinates(); canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null); } } /** * Called if you change the configuration like open the keypad. */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * Called on creation of the SurfaceView. * Which could be on first start or relaunch. */ @Override public void surfaceCreated(SurfaceHolder holder) { if (!_thread.isAlive()) { _thread = new RockScissorsPaperThread(this); } _thread.setRunning(true); _thread.start(); } /** * Called if the SurfaceView should be destroyed. * We try to finish the game loop thread here. */ @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; _thread.setRunning(false); while (retry) { try { _thread.join(); retry = false; } catch (InterruptedException e) { // we will try it again and again... } } Log.i("thread", "Thread terminated..."); } }
September 22, 2012
where to placed file which is to be ziped

where we supposed to placed file that we wanted to ziped?