Language: ChineseGermanSpanishFrenchDutchItalianRussian
123 Flash Chat Forums

Go Back   TOPCMM Community > SWFKit > SWFKit Support

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-25-2009, 04:34 AM
Junior Member
 
Join Date: Jan 2009
Posts: 9
Default Issue using LiteUnzip "UnzipItemToBuffer"

Hi I am using SWFKit to load in data from an encrypted .zip file which is generated after our application is shipped. There is one caveat however: the data cannot be written disk. So using the LiteUnzip example I found on the boards here, I made the following bit of code:

Code:
struct { 
 	long dwLowDateTime; 
 	long dwHighDateTime; 
} FILETIME;

struct {
	long Index; 
 	long Attributes; 
 	FILETIME AccessTime; 
 	FILETIME CreateTime;
 	FILETIME ModifyTime;
 	long CompressedSize; 
 	long UncompressedSize;
	char Name[260]; 
} zipEntry;

/*
	Error Codes
	
	These are the standard error codes for LiteUnzip functions
*/
/*
var ZR_OK=			0;		// Success
// The following come from general system stuff (e.g. files not openable)
var ZR_NOFILE=		1;		// Can't create/open the file
var ZR_NOALLOC=		2;		// Failed to allocate memory
var ZR_WRITE=		3;		// A general error writing to the file
var ZR_NOTFOUND=	4;		// Can't find the specified file in the zip
var ZR_MORE=		5;	// There's still more data to be unzipped
var ZR_CORRUPT=		6;		// The zipfile is corrupt or not a zipfile
var ZR_READ=		7;		// An error reading the file
var ZR_NOTSUPPORTED=8;		// The entry is in a format that can't be decompressed by this Unzip add-on
// The following come from mistakes on the part of the caller
var ZR_ARGS=		9;		// Bad arguments passed
var ZR_NOTMMAP=		10;		// Tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
var ZR_MEMSIZE=		11;		// The memory-buffer size is too small
var ZR_FAILED=		12;		// Already failed when you called this function
var ZR_ENDED=		13;		// The zip creation has already been closed
var ZR_MISSIZE=		14;		// The source file size turned out mistaken
var ZR_ZMODE=		15;		// Tried to mix creating/opening a zip 
// The following come from bugs within the zip library itself
var ZR_SEEK=		16;		// trying to seek in an unseekable file
var ZR_NOCHANGE=	17;		// changed its mind on storage, but not allowed
var ZR_FLATE=		18;		// An error in the de/inflation code
var ZR_PASSWORD=	19;		// Password is incorrect
*/

var unLiteZip = getAppDir() + "LiteUnzip.dll" ;//getAdditionalFile("LiteUnzip.dll");
dllimport unLiteZip stdcall long UnzipOpenFileA(long*, String, String); 
dllimport unLiteZip stdcall long UnzipGetItemA(long, zipEntry*) ;
dllimport unLiteZip stdcall long UnzipGetItemW(long, zipEntry*) ;
dllimport unLiteZip stdcall long UnzipItemToFileA(long, String, zipEntry*);
dllimport unLiteZip stdcall long UnzipItemToBuffer(long, char*, long, zipEntry*);
dllimport unLiteZip stdcall long UnzipFindItemA(long, zipEntry*, Boolean);
dllimport unLiteZip stdcall long UnzipClose(long) ;

function unzip(archivename, filename, password, resultarray)
{
	var archivepath = "";
	var resultcode =0;
	var obj = new Object;
	obj.value = 0;
	
	archivepath = getAppDir() + archivename;
	
	resultcode = UnzipOpenFileA(obj, archivepath , password); 	
	if (resultcode != 0)
	{
		Dialogs.msgBox("UnzipOpenFile - " + resultcode);
		return resultcode;
	}

	var handle = obj.value;
	var ze = new Struct(zipEntry);
	var parsedfilename = false;
	ze.Index = 0;
	//ze.Name = filename;
	obj.value = ze;
	
	while (UnzipGetItemA(handle,obj)==0)
	{
		var name=new String(obj.value.Name);
		var filenametest=new String(filename);
		var name_array = name.split("\\");
		name=new String(name_array.slice(-1));
				
		if (filenametest==name)
		{
			parsedfilename=true;
			break;
		}
		ze.Index += 1;
		obj.value = ze;
		obj.value.Name="";
		continue;
	}
	
	if (!parsedfilename)
	{
		return 4;
	}
	
 	var buffer = new StringStream;
 	var bufferref = new Object;
 	bufferref.value = buffer;
 	buffer.length=obj.value.UncompressedSize;
 	buffer.putPos =0;
 
 	
 
 	Dialogs.msgBox("handle = '"+handle+"' length="+buffer.length+" name="+obj.value.Name );
 
 	resultcode = UnzipItemToBuffer(handle, bufferref, buffer.length, obj);
 	buffer.saveToFile("c:\\bufferout.dat");
 	if (resultcode != 0)
	{
		Dialogs.msgBox("UnzipItemToFileBufferPtr - "+resultcode+" len="+buffer.length);
		UnzipClose(handle);
		return resultcode;
	}

	UnzipClose(handle);

	// If there was no error then we return the contents of the file.
 	return buffer.read(buffer.length); 	
}
The problem is that when I am reading in data from UnzipItemToBuffer, I get the error "5" which is the what LiteUnzip tells you there are subsequent reads to make to read in all the compressed data. The size of buffer is correct, (in my case I am reading in the entire compressed entry,) not surprisingly all the data in the test output file "c:\bufferout.dat" is 0's. Thus my guess is I am somehow defining the function wrong, or incorrectly specifying the buffer reference.
Reply With Quote
  #2 (permalink)  
Old 06-07-2009, 02:27 PM
RJS RJS is offline
Member
 
Join Date: Sep 2003
Posts: 56
Default

Any suggestions please SWKKit. I'd like a solution to this too.
Reply With Quote
  #3 (permalink)  
Old 06-14-2009, 08:43 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default

Code:
struct { 
 	long dwLowDateTime; 
 	long dwHighDateTime; 
} FILETIME;

struct {
	long Index; 
 	long Attributes; 
 	FILETIME AccessTime; 
 	FILETIME CreateTime;
 	FILETIME ModifyTime;
 	long CompressedSize; 
 	long UncompressedSize;
	char Name[260]; 
} zipEntry;

/*
	Error Codes
	
	These are the standard error codes for LiteUnzip functions
*/
/*
var ZR_OK=			0;		// Success
// The following come from general system stuff (e.g. files not openable)
var ZR_NOFILE=		1;		// Can't create/open the file
var ZR_NOALLOC=		2;		// Failed to allocate memory
var ZR_WRITE=		3;		// A general error writing to the file
var ZR_NOTFOUND=	4;		// Can't find the specified file in the zip

var ZR_CORRUPT=		6;		// The zipfile is corrupt or not a zipfile
var ZR_READ=		7;		// An error reading the file
var ZR_NOTSUPPORTED=8;		// The entry is in a format that can't be decompressed by this Unzip add-on
// The following come from mistakes on the part of the caller
var ZR_ARGS=		9;		// Bad arguments passed
var ZR_NOTMMAP=		10;		// Tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
var ZR_MEMSIZE=		11;		// The memory-buffer size is too small
var ZR_FAILED=		12;		// Already failed when you called this function
var ZR_ENDED=		13;		// The zip creation has already been closed
var ZR_MISSIZE=		14;		// The source file size turned out mistaken
var ZR_ZMODE=		15;		// Tried to mix creating/opening a zip 
// The following come from bugs within the zip library itself
var ZR_SEEK=		16;		// trying to seek in an unseekable file
var ZR_NOCHANGE=	17;		// changed its mind on storage, but not allowed
var ZR_FLATE=		18;		// An error in the de/inflation code
var ZR_PASSWORD=	19;		// Password is incorrect
*/

var ZR_MORE=		5;	// There's still more data to be unzipped

var unLiteZip = getAppDir() + "LiteUnzip.dll" ;//getAdditionalFile("LiteUnzip.dll");
dllimport unLiteZip stdcall long UnzipOpenFileA(long*, String, String); 
dllimport unLiteZip stdcall long UnzipGetItemA(long, zipEntry*) ;
dllimport unLiteZip stdcall long UnzipGetItemW(long, zipEntry*) ;
dllimport unLiteZip stdcall long UnzipItemToFileA(long, String, zipEntry*);
// UnzipItemToBuffer(int handle, char *buffer, long compressedSize, zipEntry* ze)
// handle - handle of the zip file
// buffer - memory to hold the uncompressed data
// compressedSize - its the COMPRESSED data size of the item you want
dllimport unLiteZip stdcall long UnzipItemToBuffer(long, char*, long, zipEntry*);
dllimport unLiteZip stdcall long UnzipFindItemA(long, zipEntry*, Boolean);
dllimport unLiteZip stdcall long UnzipClose(long) ;


function unzip(archivename, filename, password, resultarray)
{
	var archivepath = "";
	var resultcode =0;
	var obj = new Object;
	obj.value = 0;
	
	archivepath = getAppDir() + archivename;
	
	resultcode = UnzipOpenFileA(obj, archivepath , password); 	
	if (resultcode != 0)
	{
		Dialogs.msgBox("UnzipOpenFile - " + resultcode);
		return resultcode;
	}

	var handle = obj.value;
	var ze = new Struct(zipEntry);
	var parsedfilename = false;
	ze.Index = 0;
	//ze.Name = filename;
	obj.value = ze;
	
	while (UnzipGetItemA(handle,obj)==0)
	{
		var name=new String(obj.value.Name);
		var filenametest=new String(filename);
		var name_array = name.split("\\");
		name=new String(name_array.slice(-1));
				
		if (filenametest==name)
		{
			parsedfilename=true;
			break;
		}
		ze.Index += 1;
		obj.value = ze;
		obj.value.Name="";
		continue;
	}
	
	if (!parsedfilename)
	{
		return 4;
	}
	
 	var buffer = new StringStream;
 	var bufferref = new Object;
 	bufferref.value = buffer;
 	buffer.length=obj.value.UncompressedSize;
 	buffer.putPos =0; 	
 
 	Dialogs.msgBox("handle = '"+handle+"' length="+buffer.length+" name="+obj.value.Name );
 	
 	var output = new StringStream;
 	while ((resultcode = UnzipItemToBuffer(handle, bufferref, obj.value.CompressedSize, obj)) == ZR_MORE)
 	{	
 		// write decoded data
 		output.write(bufferref.value.read(obj.value.CompressedSize));
 	}
 	// write decoded data
 	output.write(bufferref.value.read(obj.value.CompressedSize));
 	output.saveToFile("c:\\bufferout.dat");
 	if (resultcode != 0)
	{
		Dialogs.msgBox("UnzipItemToFileBufferPtr - "+resultcode+" len="+buffer.length);
		UnzipClose(handle);
		return resultcode;
	}

	UnzipClose(handle);

	// If there was no error then we return the contents of the file.
 	return output; 	
}
Reply With Quote
  #4 (permalink)  
Old 06-14-2009, 08:51 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default

The UnzipItemToBuffer function is a little complex. The following is the explanation of its parameters.

// UnzipItemToBuffer(int handle, char *buffer, long compressedSize, zipEntry* ze)
// handle - handle of the zip file
// buffer - memory to hold the uncompressed data
// compressedSize - its the COMPRESSED data size of the item you want
// ze - the zip entry.

It doesn't return all decompressed data immediately. Instead, it returns ZR_MORE to tell you that there is more data and you would have to call it again. And the ze structure will hold the available decompressed data length in ze.CompressedSize, a little surprising.

So the task cannot be accomplished by just one single call. You must call it again and again until it returns OK. Every time you call it, you may want to copy the decompressed data to another StringStream buffer. However, for swfkit pro 3.4, the "write" method of the stringstream object cannot take a stringstream parameter, only a filestream object can write data from a stringstream object, so it's not easy. Thus, we built a patch for you, which supports to write data into a stringstream object from another one.

The patch is at here:
http://www.swfkit.com/download/swfkit/patch090615.zip

It also fixes the json problem.
Reply With Quote
Reply

Was this information helpful?    Yes No



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:47 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.