/*
(c) Oleg V. Dolomanov 2002-3
This source code is provided without warranty of any kind.
The author is not responsible for losses of information or any other 
damage caused by this code. You use it on your own risk. You can distribute, 
modify and incorporate this code into any non-commercial packages without 
author's permission. Acknowledgements are not necessary, but would be 
appreciated.
*/

#include <vcl.h>
#pragma hdrstop

#include "zips.h"

//---------------------------------------------------------------------------
TZipShell::TZipShell()
{
	FFiles = new TStringList;
	FDecoder = "pkunzip.exe";
}
TZipShell::~TZipShell()
{
	delete FFiles;
}
void _fastcall TZipShell::SetTmpPath(AnsiString S)
{
	FTmpPath = ExtractShortPathName(S);
	if( FTmpPath[FTmpPath.Length()] != '\\' )
		FTmpPath += '\\';
	ListFile = FTmpPath + "list.txt";
}
void _fastcall TZipShell::SetDecoder(AnsiString S)
{
	FDecoder = ExtractShortPathName(S);
}
bool _fastcall TZipShell::Initialize(AnsiString Arch)
{
	if( !FileExists(Arch) )
		return false;
	FArchive = ExtractShortPathName(Arch);
	if( FileExists(ListFile) )
	{
		FileSetAttr(ListFile, 0);
		if( !DeleteFile(ListFile) )
			return false;
	}
	AnsiString Tmp = FDecoder;
	Tmp += ' ';
	Tmp += FArchive;
	Tmp += " -@";             // pkunzip format
	Tmp += ListFile;
	if( system(Tmp.c_str()) )
	{
		return false;
	}
	if( !FileExists(ListFile) )
		return false;
	FFiles->LoadFromFile(ListFile);
	for( int i=0; i < FFiles->Count; i++ )
	{
		Tmp = FFiles->Strings[i];
		for( int j=1; j <= Tmp.Length(); j++ )
			if( Tmp[j] == '/' )
				Tmp[j] = '\\';
		FFiles->Strings[i] = Tmp;

	}
	return true;
}
void _fastcall TZipShell::MaskFiles(AnsiString Mask, AnsiString Mask1)
{
	AnsiString Tmp;
	for( int i=0; i < FFiles->Count; i++ )
	{
		Tmp = ExtractFileExt(FFiles->Strings[i]).LowerCase();
		if( (Tmp != Mask)  && (Tmp != Mask1) )
		{
			FFiles->Delete(i);
			i--;
		}
	}
}
void _fastcall TZipShell::MaskFiles(AnsiString Mask)
{
	AnsiString Tmp;
	for( int i=0; i < FFiles->Count; i++ )
	{
		Tmp = ExtractFileExt(FFiles->Strings[i]).LowerCase();
		if( Tmp != Mask )
		{
			FFiles->Delete(i);
			i--;
		}
	}
}
bool _fastcall TZipShell::Extract()
{
	if( !Files->Count )
		return false;
	if( FileExists(ListFile) )
	{
		FileSetAttr(ListFile, 0);
		if( !DeleteFile(ListFile) )
			return false;
	}
	try
	{
		FFiles->SaveToFile(ListFile);
	}
	catch(...)
	{
		Application->MessageBox("Cannot save to lst file!", "Error", MB_OK|MB_ICONERROR);
		return false;
	}
	AnsiString Tmp = FDecoder;
	Tmp += " -e -d -o ";	// extract /create dirs / overwrite
	Tmp += FArchive;
	Tmp += " @";             // pkunzip format
	Tmp += ListFile;
	Tmp += ' ';
	Tmp += FTmpPath;
	if( system(Tmp.c_str()) )
	{
		return false;
	}
	return true;
}
bool _fastcall TZipShell::GetFile(AnsiString Name)
{
	FFiles->Clear();
	FFiles->Add(Name);
	return Extract();
}

#pragma package(smart_init)

