Powered By Blogger

Friday, October 26, 2012

Simple Java Program to Read Zip Files


This program takes a Zip File Name as input ( filename matchin regex '*.zip' )
'read()' method opens a ZipFileInputStream and reads each ZipEntry. A zipEntry is a file constituent of a zipFile. for E.g : if a zipFile name = =myzip.zip contains three files, namely :
file1.txt, file2.png, file.java, then myzip.zip has three ZipEntries.

Bytes contents of each Entry is written to a ByteArrayOutput stream and saved in a HashMap.
ZipInputStream and ByteArrayOutput streams are then closed


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipFileReader {

    private File zipFile;
   
    public ZipFileReader( File zipFile ) {
        this.zipFile = zipFile;
    }

    /**
     *
     * @return
     * @throws IOException
     */
    public Map read( ) throws IOException {

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        Map byteMap = new HashMap();
       
        byte[] buffer = new byte[4096];
        ZipEntry entry = null;
   
        while ((entry = zis.getNextEntry()) != null) {
           
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            System.out.println("Extracting: " + entry);
            int numBytes;
   
            while ((numBytes = zis.read(buffer, 0, buffer.length)) != -1)
                bos.write(buffer, 0, numBytes);

            zis.closeEntry();

bos.close();
                       
            byteMap.put( entry.getName(), bos.toByteArray() );
            }
        return byteMap;
    }
   
  }

Wednesday, February 8, 2012

Back tracking procedure to find the exit through a maze

Here's a backtracking program to find the exit through a maze.
Maze is denoted by a 2D matrix with elements 0 and 1.
0 denotes a 'way ahead into the maze' and '1 denotes a blockage'
Let the matrix be :
static int[][] maze = new int[][] {
{ 0,0,0,0,1,0,0,0 },
{ 0,1,1,0,1,0,1,1 },
{ 0,1,1,0,1,0,0,1 },
{ 0,1,0,0,1,1,0,1 },
{ 0,1,0,0,0,1,0,1 },
{ 0,1,0,1,0,1,0,1 },
{ 0,1,0,0,0,1,0,1 },
{ 1,1,1,0,1,0,0,1 },
{ 1,1,1,0,0,0,1,1 }
};

Traversal steps :
1. Visit a particular index with value=0
2. Look in all the four directions to find out the all the valid directions in which a further movement can be made
3.Store all the current valid directions.
4. Make a movement into one these.
5. If arrived to the exit , then quit
6. Else move back and try other of stored directions

The procedure is recursive and current valid directions are stored on a stack.

Pathtoexit is the final matrix that denotes the correct path traversed to get to the exit;
Program output :
###############################
printing the co-ordinates of the right path
Success : x=0 y=7
Success : x=0 y=6
Success : x=0 y=5
Success : x=1 y=5
Success : x=2 y=5
Success : x=2 y=6
Success : x=2 y=7
Success : x=3 y=7
Success : x=4 y=7
Success : x=4 y=6
Success : x=5 y=6
Success : x=6 y=6
Success : x=7 y=6
Success : x=7 y=5
Success : x=8 y=5
Success : x=8 y=4
Success : x=8 y=3
Success : x=7 y=3
Success : x=6 y=3
Success : x=6 y=4
Success : x=5 y=4
Success : x=4 y=4
Success : x=4 y=3
Success : x=3 y=3
Success : x=2 y=3
Success : x=1 y=3
Success : x=0 y=3
Success : x=0 y=2
Success : x=0 y=1
----Input maze, 0=way ahead, 1=blockage------
0 0 0 0 1 0 0 0
0 1 1 0 1 0 1 1
0 1 1 0 1 0 0 0
0 1 0 0 1 1 1 0
0 1 0 0 0 1 0 0
0 1 0 1 0 1 0 1
0 1 0 0 0 1 0 1
1 1 1 0 1 0 0 1
1 1 1 0 0 0 1 1

---Exit path. 0 denotes the path to exit ------

0 0 0 0 1 0 0 0
1 1 1 0 1 0 1 1
1 1 1 0 1 0 0 0
1 1 1 0 1 1 1 0
1 1 1 0 0 1 0 0
1 1 1 1 0 1 0 1
1 1 1 0 0 1 0 1
1 1 1 0 1 0 0 1
1 1 1 0 0 0 1 1

######
----Below is the actual java code ----


/**
*
* @author Nitesh
*
*/
public class BackTrackingMaze {

public static void main( String[] args ) {
BackTrackingMaze bm = new BackTrackingMaze();
System.out.println("printing the co-ordinates of the right path");
bm.move( 0, 0, maze, -1, -1 );

System.out.println("-----------------Input maze, 0=way ahead, 1=blockage------------------");
bm.printInputMaze();

System.out.println("\n----------------- 0 denotes the path to exit --------------------- \n");
bm.printExitPath();
}

static int[][] maze = new int[][] {
{ 0,0,0,0,1,0,0,0 },
{ 0,1,1,0,1,0,1,1 },
{ 0,1,1,0,1,0,0,0 },
{ 0,1,0,0,1,1,1,0 },
{ 0,1,0,0,0,1,0,0 },
{ 0,1,0,1,0,1,0,1 },
{ 0,1,0,0,0,1,0,1 },
{ 1,1,1,0,1,0,0,1 },
{ 1,1,1,0,0,0,1,1 }
};

static int startX = 0;
static int startY = 0;
static int endX = 0;
static int endY = maze[ 0].length - 1;

static int[][] pathtoexit = new int[ maze.length ][ maze[0].length ];

static {
for ( int i = 0; i < maze.length; i++ ) {
for ( int j = 0; j < maze[i].length; j++ ) {
//pathtoexit[i][j] = maze[i][j];
pathtoexit[i][j] = 1;
}
}
pathtoexit[ startX ][ startY ] = 0;
}



int[] exitIndex = new int[] { endX, endY };

private boolean success = false;

private int dead_end = 2;
private int found_exit = 0;
private int dead_end_maze = 3;

private boolean isFreedom( int x, int y, int[][] maze) {
        //return (x == exitIndex[0] && y == exitIndex[1]);
        return ( y == maze[0].length-1 ) && ( maze[x][y] == 0 );
    }

    /**
     *
     * @param x, current x co-ordinate  into  this direction ( downward for the input matrix ) .... equivalent to lookSoth
     *                                                  |
     *                                                  |
     *                                                  |
     *                                                 \/
     *                                               
     * @param y, current y ordinate, into -----------> this direction ( horizontal direction ) .... equivalen to lookEast
     * @param maze
     * @param px
     * @param py
     * @return
     */public int move( int x, int y, int[][] maze, int px, int py ) {

if ( isFreedom ( x, y, maze ))
            success = true;

if ( success ) { return found_exit; }

// there are more valid moves
Stack stk = new Stack();
//x = row index
// y= column index

int[] north = lookNorth( x, y, maze, px, py );
if ( north != null ) stk.push( north );

int[] west = lookWest( x, y, maze, px, py );
if ( west != null ) stk.push( west );

int[] south = lookSouth( x, y, maze, px, py );
if ( south != null ) stk.push( south );

int[] east = lookEast( x, y, maze, px, py );
if ( east != null ) stk.push( east );

if ( stk.isEmpty() ) {
System.out.println( "dead end : row=" + x + " col=" + y );
return dead_end;
}
int[] out = null;
while ( !success && !stk.isEmpty() ) {
out = stk.pop();
int ret = move( out[0], out[1], maze, x, y );
if ( ret == dead_end ) {
System.out.println( "dead end returned: row=" + x + " col=" + y );
}
}

if ( success ) {
System.out.println("Success :" + " x=" + out[0] + " y=" + out[1]);
pathtoexit[ out[0] ][ out[1] ] = 0;
return found_exit;
}

System.err.println("Dead end maze, last index: " + " x=" + out[0] + " y=" + out[1] );
return dead_end_maze;

}

public int[] lookEast( int x, int y, int[][] maze, int px, int py ) {
int k = ++y;
return ( k == py || k == maze[0].length || maze[x][k] == 1 ) ? null : new int[] { x, k };
}

public int[] lookSouth( int x, int y, int[][] maze, int px, int py ) {
int k = ++x;
return ( k == px || k == maze.length || maze[k][y] == 1 ) ? null : new int[] { k, y };
}

public int[] lookNorth( int x, int y, int[][] maze, int px, int py ) {
int k = --x;
return ( k == px || k == -1 || maze[k][y] == 1 ) ? null : new int[] { k, y };
}

public int[] lookWest( int x, int y, int[][] maze, int px, int py ) {
int k = --y;
return ( k == py || k == -1 || maze[x][k] == 1 ) ? null : new int[] { x, k };
}

private void printExitPath() {
for ( int i = 0; i < pathtoexit.length; i++ ) {
for ( int j = 0; j < pathtoexit[i].length; j++ ) {
System.out.print( pathtoexit[i][j] + " " );
}
System.out.println("");
}

}

private void printInputMaze() {
for ( int i = 0; i < maze.length; i++ ) {
for ( int j = 0; j < maze[i].length; j++ ) {
System.out.print( maze[i][j] + " " );
}
System.out.println("");
}

}


}

Tuesday, January 31, 2012

InMemory Transpose of a sqaure matrix

/* swap every row with the corresponding column-- Let this step be called exchange
* After every exchange operation , size of matrix decreases by 1.
So further exchanges are to be made on this new reduced matrix.
So let suppose a 5 x 5 matrix, whose transpose we need to find
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

After first exchange( swapping values of row 0 with column 0 )
1 6 11 16 21
2 7 8 9 10
3 12 13 14 15
4 17 18 19 20
5 22 23 24 25

So the reduced matrix upon with subsequent exchanges are to made is a 4 x 4

7 8 9 10
12 13 14 15
17 18 19 20
22 23 24 25

Hence, the exchange Method has a loop 'for ( int k = i + 1; k < len; k++ ) {'

int[][] interchange( int i, int[][] in ) {
int[] tmp = in[i];

int len = tmp.length;
for ( int k = i + 1; k < len; k++ ) {

int a = in[k][i];
in[k][i] = in[i][k];
in[i][k] = a;

}
return in;

}


**
*/

###### Complete Code #############################

public class TransposeOfMatrixInMemory {

public static void main( String[] args ) {
int[][] in1 = new int [][] {
{ 1, 2, 3, 4, 5 } ,
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{16, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25 }

};
TransposeOfMatrixInMemory obj = new TransposeOfMatrixInMemory();
int[][] in = obj.transpose( in1 ) ;
for ( int i = 0 ;i < in.length; i++ ) {
for ( int j = 0; j < in[i].length ; j ++ ) {
System.out.print( in[i][j] + " " );
}
System.out.println();
}

}

/**
* Matrix is a square matrix
* @param in
* @return
*/
public int[][] transpose( int[][] in ) {
if ( in == null || in.length == 0 ) return null;

int [][] tmp = in;


int len = tmp.length;
for ( int i = 0; i < len; i++ ) {
tmp = interchange( i, in );
}
return tmp;
}

int[][] interchange( int i, int[][] in ) {
int[] tmp = in[i];

int len = tmp.length;
for ( int k = i + 1; k < len; k++ ) {

int a = in[k][i];
in[k][i] = in[i][k];
in[i][k] = a;

}
return in;

}



}

Friday, January 20, 2012

program to Unzip a file

/**
* This Unzip Unzips a zippedfile and returns the list of filesname unzipped
* @author Nitesh
*
*/
public class Unzip {

public static void main(String[] args) {
String dir = "E:\\data\\ad";
String zipFile = "zip.zip";
try{

Unzip list = new Unzip( );
List files = getUnZippedFile( dir, zipFile );
System.out.println("Unzipped file : ----------------");
for( File f : files ) {
System.out.println( f.getAbsolutePath() );
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* Unzip all the files of the for 'zipfilename'. New folder created with the same name all the inner files
* written to the folder under the 'dir' directory
* @param dir
* @param zipFileName
* @return
*/
public static List getUnZippedFile(String dir, String zipFileName ) {

if ( dir == null || dir.length() == 0 || zipFileName == null || zipFileName.length() == 0 ) {
System.out.println("Cannot unzip. No Zip files found at location at directory : " + dir + " , filename :" + zipFileName );
return null;
}
if ( ! zipFileName.endsWith( ".zip") ) {
System.out.println("Unzipping fails. This file does not seem to be a ZipFile : " + zipFileName );
return null;
}
List unzipedFiles = null;
try {

File f = new File( dir, zipFileName );

byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(
new FileInputStream( f ));

zipentry = zipinputstream.getNextEntry();
unzipedFiles = new ArrayList();

while (zipentry != null) {
//for each entry to be extracted
/* if the zip folder is test.zip and has file atest.csv then entryName = test/atest.csv */
String entryName = zipentry.getName();
System.out.println("entryname "+entryName);
int n;
FileOutputStream fileoutputstream;
File newFile = new File( entryName );
String innerZipFile = newFile.getName(); // e.g atest.csv

String parent = newFile.getParent(); // this determines if it is a folder zip or a file zip ??? strange , yep !
File ff = null;
if ( parent == null ) {
// This is the case of file zip. create a folder with the same name as 'zipfileName'
ff = new File( dir, zipFileName.substring(0, zipFileName.lastIndexOf("." ) ) );
}else {
ff = new File( dir, newFile.getParent() ); // create folder dir + test
}

if ( ! ff.exists() ) {
ff.mkdir() ;
}
File outputFile = new File( ff, innerZipFile );
fileoutputstream = new FileOutputStream( outputFile );

while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
fileoutputstream.write(buf, 0, n);
}

fileoutputstream.close();
zipinputstream.closeEntry();

unzipedFiles.add( outputFile );

zipentry = zipinputstream.getNextEntry();

}//while

zipinputstream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return unzipedFiles;
}
}

Monday, January 16, 2012

Print continuous longest substring without repeting chars from a charcter array

Longest consecutive substring without repetition


To find the longest substring without repetition of characters from an array of characters.
Let's suppose the string is "helloforhellowhy"
There are 1 continuous substring, with maximum length, without char repetition
"forhel" -- length =6

Another Example String: "nevertheless"  
 Maximum length without repetition: "verth" -- length = 5

String : aback
output: back -- length = 4

Belwo program prints the first string. It can be modified a bit to print all the possible substrings with maximum length



--------------------------------------------------------------------------------------

public class LongestSubstring {

    public static int[] longestWithoutRepeat(char[] input) {
        if (input == null || input.length == 0)
            return null;
        HashMap map = new HashMap<>(input.length);

        int[] lastIndices = new int[] { 0, 0 };
        int[] currentIndices = new int[] { 0, 0 };
        map.put(Character.valueOf(input[0]), 0);

        int j = input.length;
        int i = 1;

        while (i < j) {
            char c = input[i];
            Integer matchingCharIndex = null;

            if ((matchingCharIndex = map.get(Character.valueOf(c))) != null) {
                map.clear();
                updateLastIndices(lastIndices, currentIndices);
                currentIndices[0] = currentIndices[1] = matchingCharIndex.intValue() + 1;
                i = currentIndices[1];
                continue;
            }
            map.put(Character.valueOf(c), i);
            currentIndices[1] = i++;
        }
        updateLastIndices(lastIndices, currentIndices);
        return lastIndices;
    }

    private static int[] updateLastIndices(int[] lastIndices,
            int[] currentIndices) {
        if ((currentIndices[1] - currentIndices[0]) > (lastIndices[1] - lastIndices[0])) {
            lastIndices[1] = currentIndices[1];
            lastIndices[0] = currentIndices[0];
        }
        return lastIndices;
    }

    private static void print(char[] input, int[] lastIndices) {
        System.out.println("Greatest len = "
                + (lastIndices[1] - lastIndices[0] + 1));
        for (int m = lastIndices[0]; m <= lastIndices[1]; m++) {
            System.out.print(input[m]);
        }
    }

    public static void main(String[] args) {

        String test = "helloforhellowhy"; // forhel, 6 passed
        String test0 = "hellofaorhellowhyasmn"; // lowhyasmn 9, passed
        String test1 = "neverthaless"; // verthal 7, passed
        String test2 = "eee"; // e, paseed
        String test3 = "heeeeiop"; // eiop, passed
        String test4 = "343jbb6n78opnei"; // b6n78op7
        String test5 = "abcabcde";
        String test6 = "aback";
        String test7 = "hellofa";
        String test8 = "abcdefcghfa"; // abcdef 6
        String test9 = "abadbg";
        String test10 = "abakk";
       
        String input = test10;
        System.out.println("Input array = " + input + " ,Length="
                + input.length());
        int[] lastIndices = longestWithoutRepeat(input.toCharArray());

        print(input.toCharArray(), lastIndices);
    }
}