Friday, May 21, 2010

Extract Corrupt Zip File By Java

I received an zip file from customer. I have tried to extract with WinZip, but unable to extract zip file because that zip file was corrupt.

So I have tried to extract with Java Zip API and got the success. The code is listed below:

I am sure that the zip file having 5% to 10% part corrupt will be unzip with this code, but if it is having more corrupt then the code will not work.



import java.io.*;
import java.util.zip.*;

class ExtractCorruptZip {
public void extractZipFiles(String fileName) {
try {
String zipDir = "c:\\zip\\";
//String zipDir = "/home/usr/amit/zip/";
byte[] by = new byte[2048];
ZipInputStream ziStream = null;
ZipEntry zipEntry;
ziStream = new ZipInputStream(new FileInputStream(fileName));

zipEntry = ziStream.getNextEntry();
int count=1;
while (zipEntry != null) {
String entry = zipDir + zipEntry.getName();
entry = entry.replace('/', File.separatorChar).replace('\\', File.separatorChar);
int n;
FileOutputStream fOTStream;
File newFile = new File(entry);
if (zipEntry.isDirectory()) {
if (!newFile.mkdirs()) {
break;
}
zipEntry = ziStream.getNextEntry();
continue;
}
fOTStream = new FileOutputStream(entry);
while ((n = ziStream.read(by, 0, 2048)) > -1) {
fOTStream.write(by, 0, n);
}
System.out.println("Working....."+(++count));
fOTStream.close();
ziStream.closeEntry();
zipEntry = ziStream.getNextEntry();
}
ziStream.close();
System.out.println("DONE!!!!!");
} catch(IOException ioe) {
ioe.printStackTrace();
}
}

public static void main(String[] ss) {
try {
String fileName = "w1.zip";
ExtractCorruptZip extractCorruptZip = new ExtractCorruptZip();
extractCorruptZip.extractZipFiles(fileName);
} catch (Exception ex) {
ex.printStackTrace();
}
}


}

Tuesday, May 18, 2010

Zend Framework setup

How to setup Zend Framework.


1) download ZendFramework-1.12.0 or latest framework from zend site.
2) extract it and paste the entire directory in anywhere in your system. in my case
i have copied it in my "C:\wamp\www" folder e.g. "C:\wamp\www\ZendFramework-1.12.0"

3) set envirenment path as "C:\wamp\www\ZendFramework-1.12.0\bin"  and also
set path of your PHP installation directory of php.exe file e.g. "C:\wamp\bin\php\php5.4.3"
4) open dos prompt window and execute listed below command:
zf create project <projectName>
e.g. zf create project zfproj
5) copy "Zend" directory from "C:\wamp\www\ZendFramework-1.12.0\library\" into
your new project's library folder e.g. "C:\wamp\www\zfproj\library"

6) now start your wamp or any other php server and open this url in your browser
http://localhost:90/zfproj/public/
where "zfproj" is the newly created zend project and "public" is the initial home directory.
now you can get the page with
"Welcome to the Zend Framework!
This is your project's main page
Helpful Links:
Zend Framework Website | Zend Framework Manual"


index with public folder
index with public folder




















Access Root Folder Of Zend Framework

Now if you don't want to use "public" folder as initial folder then follow listed below steps:
1) copy ".htaccess" and " "index.php" files from "C:\wamp\www\zfproj\public" folder into
root folder e.g. "C:\wamp\www\zfproj" folder.
2) open "C:\wamp\www\zfproj\index.php" file.
make comment this line:
//defined('APPLICATION_PATH') || define('APPLICATION_PATH',realpath(dirname(__FILE__).'/../application'));
copy paste this listed below code:
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

now open this url "http://localhost:90/zfproj/" in your web browser.
now you will get the index page without "public" folder.



index without public folder
index without public folder





















Saturday, May 15, 2010

Image tagging in PHP

Image tagging in PHP

index.php

<html>
<head>
<script type='text/javascript' src='js/jquery.min.js'></script>
<script type='text/javascript' src='js/jquery-ui.min.js'></script>
<script type='text/javascript' src='js/jquery.tag.js'></script>
<link media="screen" rel="stylesheet" href="css/jquery.tag.css" type="text/css" />
<link media="screen" rel="stylesheet" href="css/jquery-ui.custom.css" type="text/css" />
</head>
<body>

<?php $image_name="simp.jpg";?>
    <img src="<?php echo $image_name;?>" id="img" />
    <?php //set here current image id
    $img_id="1";?>
</body>
<script>

$(document).ready(function() {
    $("#img").tag ( {
        //clickToTag: false,
        //canTag:false,
    //canDelete: false,
   
    save: function(width,height,top_pos,left,label,the_tag) {
    $.post("ajax.php",{'action':'save','img_id':<?php echo $img_id;?>,'width':width,'height':height,'top':top_pos,'left':left,'label':label},function(id) {
                    the_tag.setId(id);
                });
                                                            },
    remove: function(id) {
    $.post("ajax.php",{'action':'delete','id':id});
                         }
                });
           
    $.getJSON("ajax.php",{'action':'list','img_id':<?php echo $img_id;?>},function(tags) {
                $.each(tags, function(key,tag) {
                    $("#img").addTag(tag.width,tag.height,tag.top,tag.left,tag.label,tag.id);
                });
            });
    });
    </script>
</html>

ajax.php
<?php
/*
CREATE TABLE `picture` (
  `img_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`img_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

insert  into `picture`(`img_id`,`name`) values (3,'alexander_the_great1.jpg'),(4,'black_holes.jpg'),(5,'lion-roaring.jpg'),(6,'M16Full.jpg'),(7,'picpower.jpg');

DROP TABLE IF EXISTS `tag`;
CREATE TABLE `tag` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `img_id` int(11) DEFAULT NULL,
  `label` varchar(100) CHARACTER SET utf8 NOT NULL,
  `width` int(11) NOT NULL,
  `height` int(11) NOT NULL,
  `top` int(11) NOT NULL,
  `left` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
insert  into `tag`(`id`,`img_id`,`label`,`width`,`height`,`top`,`left`) values (7,1,'testtest',100,100,31,75);
*/
mysql_connect("localhost","root","");
mysql_select_db("test");

    switch($_REQUEST['action']){
        case 'list':
            $tags = array();
            $img_id=$_REQUEST['img_id'];
            $query = mysql_query("select * from tag where img_id=$img_id");
            while($row = mysql_fetch_array($query)){
                $tags[] = array("id"=>$row['id'],
                                "label"=>$row['label'],
                                "width"=>$row['width'],
                                "height"=>$row['height'],
                                "top"=>$row['top'],
                                "left"=>$row['left']);
            }
            echo json_encode($tags);
        break;
   
        case 'delete':
            mysql_query("delete from tag where id = ".$_REQUEST['id']);
        break;
       
        case 'save':
            mysql_query("insert into tag (`img_id`,`width`,`height`,`top`,`left`,`label`) values (
                ".$_REQUEST['img_id'].",
                ".$_REQUEST['width'].",
                ".$_REQUEST['height'].",
                ".$_REQUEST['top'].",
                ".$_REQUEST['left'].",
                '".$_REQUEST['label']."'
            )") or die(mysql_error());
            echo mysql_insert_id();           
        break;
    }
?>

Download here

Wednesday, May 12, 2010

How to check if image completely loaded.

How to check if image completely loaded.

    $("#imgId").attr("src","img.jpg").load(function() { 
        // put here code that will execute after image loaded.
    }); 

PHP and Zend Certification

PHP and Zend Certification

I have got the test certification for PHP and Zend from uCertify
This uCertify Prepkit helps the user to learn and practice
before taking the actual IT certification exam in different technologies.


It is very easy to install.
With using it, developer can test his/her skills and also share it with friends.

It is basically provide two parts for developer.
A) Practice Test: In This section, developer attend different
 types of questions. and valuate his/her knowledge.




B) Study Helper: If developer having some doubt about some technical
 knowledge, then this section will teach them about some technical issues
for example: PHP Basics, Security, String & Patterns and web features etc.

I think it is very good software for testing skills. There are many other testing skills in other technology from
uCertify,  Every developer should try it.
You can down load it from from uCertify

Amit Gaur