#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <tchar.h>
#include "zip.h"
#include "unzip.h"
#include "st.h"

using namespace std;

class UnzipCommand {
public:
   int run(int argc, char ** argv) {
      vector< string > file_vec; 
      int i;

      if(argc < 2) 
         throw "usage: unzip <zip-file-name>";


      for(i = 1; i < argc; i++) {
         file_vec.push_back(argv[i]);
      }

      for(i = 0; i < file_vec.size(); i++) { 
         HZIP hz = OpenZip(file_vec[i].c_str(),0);

         if(hz == NULL)
            throw "unable to open zip file...";

         cout << "open: " << file_vec[i] << endl;

         ZIPENTRY ze;

         GetZipItem(hz,-1,&ze);
      
         int numitems = ze.index;

         for(i = 0; i < numitems; i++) {
            GetZipItem(hz,i,&ze);

            cout << "uncompress: " << ze.name << endl;

            UnzipItem(hz,i,ze.name);
         }

         CloseZip(hz);
      }

      cout << "done..." << endl;

      return 0;
   }
};

#ifdef NO_MAIN
extern "C" {
extern int unzip_main(int argc, char ** argv);
}
int
unzip_main(int argc, char ** argv)
#else
int
main(int argc, char ** argv)
#endif
{
   int rc = 0;
   try {
      UnzipCommand command;
      rc = command.run(argc,argv); 
   }
   catch(const char * message) {
      cerr << "exception: " << message << endl;
      rc = -1;
   }
   catch(...) {
      cerr << "exception..." << endl;
      rc = -1;
   }
   return rc;
}
