Ejemplo n.º 1
0
//----------------------------------------------------------------//
int	zipfs_fclose ( ZIPFSFILE* fp ) {

	int result = 0;

	if ( fp ) {
		ZIPFSFile* file = ( ZIPFSFile* )fp;
		result = ( file->mIsArchive ) ? ZIPFSZipStream_Close ( file->mPtr.mZip ) : fclose ( file->mPtr.mFile );
		memset ( fp, 0, sizeof ( ZIPFSFile ));
		free ( file );
	}
	return result;
}
Ejemplo n.º 2
0
//----------------------------------------------------------------//
ZIPFSZipStream* ZIPFSZipStream_Open ( ZIPFSZipFile* archive, const char* entryname ) {

	int result;
	FILE* file = 0;
	ZIPFSZipStream* self = 0;
	ZIPFSZipFileEntry* entry;
	FileHeader fileHeader;
	
	entry = ZIPFSZipFile_FindEntry ( archive, entryname );
	if ( !entry ) goto error;

	file = fopen ( archive->mFilename, "rb" );
	if ( !file ) goto error;

    self = ( ZIPFSZipStream* )calloc ( 1, sizeof ( ZIPFSZipStream ));

	self->mFile = file;
	self->mEntry = entry;
	// finfo->entry = (( entry->symlink != NULL ) ? entry->symlink : entry );

	// seek to the base of the zip file header
	result = fseek ( file, entry->mFileHeaderAddr, SEEK_SET );
	if ( result ) goto error;

	// read local header
	result = read_file_header ( file, &fileHeader );
	if ( result ) goto error;
	
	// sanity check
	if ( fileHeader.mCrc32 != entry->mCrc32 ) goto error;

	// skip the extra field, etc.
	result = fseek ( file, fileHeader.mNameLength + fileHeader.mExtraFieldLength, SEEK_CUR );
	if ( result ) goto error;

	// this is the base address of the compressed file data
	self->mBaseAddr = ftell ( file );
    
    // looks like all systems are go, so time to set up the buffers (or not)
    result = ( entry->mUncompressedSize <= ZIP_STREAM_BUFFER_MAX ) ? ZIPFSZipStream_FullyCache ( self ) : ZIPFSZipStream_InitBuffers ( self );
	if ( result ) goto error;
    
	return self;

error:

	if ( self ) {
		ZIPFSZipStream_Close ( self );
	}
	return 0;
}
Ejemplo n.º 3
0
//----------------------------------------------------------------//
ZIPFSFILE* zipfs_freopen ( const char* filename, const char* mode, ZIPFSFILE* fp ) {

	ZIPFSFile* result = 0;
	ZIPFSFile* file = ( ZIPFSFile* )fp;
	ZIPFSVirtualPath* mount;

	if ( !file ) return 0;

	if ( file->mIsArchive ){
		ZIPFSZipStream_Close ( file->mPtr.mZip );
	}
	else {
		fclose ( file->mPtr.mFile );
	}
	
	memset ( fp, 0, sizeof ( ZIPFSFile ));

	filename = zipfs_get_abs_filepath ( filename );
	mount = find_best_virtual_path ( filename );

	if ( mount ) {
		
		if ( mode [ 0 ] == 'r' ) {
			
			ZIPFSZipStream* zipStream;
			
			filename = ZIPFSVirtualPath_GetLocalPath ( mount, filename );
			zipStream = ZIPFSZipStream_Open ( mount->mArchive, filename );
			
			if ( zipStream ) {
				file->mIsArchive = 1;
				file->mPtr.mZip = zipStream;
				result = file;
			}
		}
	}
	else {
		
		FILE* stdFile = fopen ( filename, mode );
		
		if ( stdFile ) {
			file->mPtr.mFile = stdFile;
			result = file;
		}
	}

	return ( ZIPFSFILE* )result;
}