Pages

Saturday, November 5, 2011

Zip/Unzip using Windows Shell

Okay, so here I was suppose to write a module for compressing / decompressing files in windows. I went through a lot of stuff over the net and found some open source libraries to do it. Out of those I narrowed down to the one provided by Info-Zip. Although no longer actively maintained, this is in existence for a long time and is known to be stable. This is used in various forms in a number of programs including Mac OS X.

However, on Windows there is another way to zip files free of cost. I found this article - Easily zip / unzip files using Windows Shell32 on which provides a neat way to use windows routines to zip and unzip files programmatically. The code provided is in VB but since my requirement was in c++, I wrote a small c++ module which is shared here. I found the discussion and code snippets provided here to be very useful.

Zip
To compress and zip files first we need to create the required zip file. The code below creates “test.zip” in "C:"

Code Snippet
------------------------------------------------------------------------------------------------------------------------
// Create Zip file
BYTE startBuffer[] = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
FILE *f = fopen("C:\\test.zip", "wb");
fwrite(startBuffer,sizeof(startBuffer),1,f);
fclose(f);
------------------------------------------------------------------------------------------------------------------------

Now that our zip file is created, lets add the files to be compressed inside this zip.
For this we use the CopyHere function wherein we treat our newly created zip file as a folder.

Code Snippet
------------------------------------------------------------------------------------------------------------------------
BSTR source = L"C:\\test.txt\0\0";
BSTR dest = L"C:\\test.zip\\\0\0";

HRESULT hResult;
IShellDispatch *pISD;
Folder *pToFolder = NULL;
VARIANT vDir, vFile, vOpt;

CoInitialize(NULL);

hResult = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD);

if (SUCCEEDED(hResult))
{

VariantInit(&vDir);
vDir.vt = VT_BSTR;
vDir.bstrVal = dest;
// Destination is our zip file
hResult = pISD->NameSpace(vDir, &pToFolder);
if (SUCCEEDED(hResult))
{
// Now copy source file(s) to the zip
VariantInit(&vFile);
vFile.vt = VT_BSTR;
vFile.bstrVal = source;
// ****NOTE**** To copy multiple files into the zip, need to create a FolderItems object (see unzip implementation below for more details)
VariantInit(&vOpt);
vOpt.vt = VT_I4;
vOpt.lVal = FOF_NO_UI;//Do not display a progress dialog box, not useful in compression

// Copying and compressing the source files to our zip

hResult = pToFolder->CopyHere(vFile, vOpt);
// CopyHere() creates a separate thread to copy files and it may happen that the main thread exits before the copy thread is initialized. So we put the main thread to sleep for a second to give time for the copy thread to start.
Sleep(1000);
pToFolder->Release();
}
pISD->Release();
}
CoUninitialize();
------------------------------------------------------------------------------------------------------------------------

Unzip is similar to zip except that we now have zip (treated as source folder) and destination which anyways is a folder. The code below currently assumes that there already exist the destination folder, however adding a code to check for a folder or create a new folder on the fly should not be difficult.
The minor change (compared to above code) in unzip comes only in identifying the source files. Here we use the folderItems, which is essentially a list of all files/folders inside the given folder, instead of a single source file.

Code Snippet
------------------------------------------------------------------------------------------------------------------------
if (SUCCEEDED(hResult))
{

Folder *pFromFolder = NULL;


VariantInit(&vFile);
vFile.vt = VT_BSTR;
vFile.bstrVal = source;


pISD->NameSpace(vFile, &pFromFolder);
FolderItems *fi = NULL;
pFromFolder->Items(&fi);


VariantInit(&vOpt);
vOpt.vt = VT_I4;
vOpt.lVal = FOF_NO_UI; // Do not display a progress dialog box

// Creating a new Variant with pointer to FolderItems to be copied

VARIANT newV;
VariantInit(&newV);
newV.vt = VT_DISPATCH;
newV.pdispVal = fi;


hResult = pToFolder->CopyHere(newV, vOpt);
Sleep(1000);
pFromFolder->Release();
pToFolder->Release();
}
------------------------------------------------------------------------------------------------------------------------

Complete project (in VS2010) is available here via CodeProject.

Saturday, August 20, 2011

Configure apache localhost to use secure HTTP (HTTPS) using mod_ssl

For all those who want to test their website on localhost using the secure HTTP protocol, here is how you can configure apache for testing purpose. I did this on Mac OSX 10.6 using apache 2.2

Guidelines provided at apple are for older version. We'll use them to generate the certificate and then configure apache for newer version.

> mkdir ~/keys
> cd ~/keys
> openssl genrsa -des3 -out server.key 1024
Remember the passphrase you provide here. It will be required in next steps.

> openssl req -new -key server.key -out server.csr
Answers to all questions are straight-forward, except for Common Name: you need to provide 127.0.0.1 (i.e. server name)

> openssl genrsa -des3 -out ca.key 1024
For simplicity, keep the passphrase same as used above

> openssl req -new -x509 -days 365 -key ca.key -out ca.crt
This again asks same Questions as in step 4, but this time for Common Name you can provide any dummy name

Find sign.sh file if you have mod_ssl package downloaded, or else retrieve it from pkg.contrib folder from latest version of package. Copy the sign.sh to "keys" folder and make it executable

>chmod +x sign.sh
>./sign.sh server.csr

Say yes ("y") to the 2 questions

>sudo mkdir /etc/apache2/ssl.key
>sudo cp -r * /etc/apache2/ssl.key/

>cd /etc/apache2/ssl.key/
>sudo cp server.key server.key.original

>sudo openssl rsa -in server.key.original -out server.key

>sudo apachectl stop

>sudo vim /etc/apache2/httpd.conf

* You may want to make a backup before editing this

- Locate and comment out Port directive to listening on 80
*Note - Commenting this line will force to use only https, leave it as is if you want to use both.

- Locate and uncomment the LoadModule ssl_module libexec/apache2/mod_ssl.so

- sudo vim /etc/apache2/extra/httpd-ssl.conf
* Again make a backup before editing

- Change ServerName from www.example.com to 127.0.0.1
- Provide your email id for ServerAdmin

- Under SSLCertification, provide path to our own ssl certificate i.e. SSLCertificateFile "/private/etc/apache2/ssl.key/server.crt"

- Also, set ServerKey with: SSLCertificateKeyFile "/private/etc/apache2/ssl.key/server.key"
- You may also set the other paths as per requirement

> sudo httpd -D SSL
> sudo apachectl start

And you now have https://127.0.0.1 running

Monday, July 25, 2011

HTML5 Games 101 - An introductory tutorial to HTML5 Games

In my last blog, I had posted the game of Snake that I developed as my first attempt in HTML5 programming and all I can say is... it was fun!

In this tutorial, I will show you how to use the basic features of HTML5 and get the simple game of Snake up and ready within a couple of hours, even if you are a beginner. All you require is some basic logic and any experience with programming is a huge plus. However, I must also admit here that this is more of a JavaScript work than HTML5, as it just uses the canvas and a couple more elements.

Okay, so lets begin!

Saturday, July 16, 2011

Snake: A beginner's attempt on HTML5 Games


HTML5 is the new buzz in the web technologies. The technology is in its infancy but with its variety of new features, it has captured everyone's attention with interesting applications and experiments across the globe. To list some check out the - interactive presentation by Seth Ladd @ Google IO 2011, use of WebGL to present geographic data or a cool way to show news, experiments like The Wilderness Downtown,  and Games like Angry Birds and those from Gopherwood Studios on Chrome web store. HTML5 is supported by all the new browsers including those on smartphones, and therefore, it has become a hot platform to develop games which developers can serve directly to the consumers bypassing the App stores. HTML5 is also expected to have enormous potential in online advertisement market (click for more details)

I decided to take a dig into this and thanks to some wonderful tutorials at html5rocks, html5games, tutsplus, links on Seth's presentation, and many more resources available, my first attempt is now on display.

I plan to put my experience in a tutorial (HTML5 Games 101) very soon. Meanwhile, share your comments and feel free to use the code and kindly acknowledge the source.

Score: 0      Level: 1


HTML5 Games 101 - by Aniruddha Loya

Sunday, July 10, 2011

Google+: Limited Invites - Actual constraints or A Marketing Gimmick?

Google+: Google's yet another attempt to socialize the users. Launched ever so silently in end of June, 2011... Google+ has caught the attention of both - techies as well as non-techies alike.

Google has previously used the limited invites strategy for its mail service to a great effect but will it work this time? All of us can speculate and only time shall provide the answer!

Here is my first take on Google+
+ves

  1. The BUZZ - Creating excitement and curiosity among people is important for the success of a product or service. It provides with the initial user base who are generally the early adapters and more often than not are also the trend setters. Thereafter, it is the usefulness of the offering which becomes important. Google has managed to create a BUZZ around its latest offering. Despite a silent launch, well placed demo, technology blogs and limited invites has done the trick to generate curiosity among the huge base social networkers (facebookers, orkuters, and others). But how long will it limit users in its trial launch?
  2. (Facebook & Twitter)+ - Google+ combines the major feature of Twitter (aka following people) and Facebook (broadcast updates) in one single platform which is a natural extension but how crucial it is for the users remains to be seen. It is worth mentioning that both the features i.e. following people and broadcasting updates were initially launched with not so successful Google Buzz. However, I did like the feature of being able to control who can follow you.
  3. The Google Power - "Powered by Google" in my opinion counts as a major factor. The simplicity of GMail was something that inspired me to move on it and still use it as my primary id and so was the case with Orkut. This was precisely the reason for not liking Facebook initially, but it has come a long way since then in terms of its user interface and offering, it has moved from just being a platform to pass time on the crazy apps and quizzes to a platform for sharing almost everything. The huge GMail userbase provides with captive clients.

-ves

  1. Limited Invites may hurt - YES! I mentioned limited invites as a good strategy in creating the BUZZ but here we are talking about a social network and people form the essence of it. The larger the user base bigger the network, so why the limited invites?
  2. Now your friends can further share your picasa albums - Till now I used picasa for sharing pics with selected people but Google+ force you to link your picasa albums with your profile enabling your connections to further share it... some users may not like it!
  3. Circle, Spark, Huddles, Hangouts, etc. Are they really very different from existing options? Can the existing players not provide such features? Doesn't feel so! Facebook has already started video chat and you can group your friends under various lists, create groups and do a group chat, plan a group event, etc. etc.
  4. Effect on your GMail account - How does creating and customizing your Google+ profile affects your GMail contacts remains to be seen. When my HTC Hero running on Android synced all my GMail contacts and mixed with my existing phone contacts from my previous Nokia phones, I wasn't particularly happy and I'm still using phone with hidden GMail contacts as it makes difficult to search the contacts with phone numbers among a list of email contacts.

Having said all this, I should also state that I haven't used Google+ till now and like many others waiting eagerly to try it out. Will post another update from a user perspective after playing around with it.