Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 06-14-2006, 06:36 PM   #1 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default #ZipLib

Does anyone have experience with this utility? I'm having some trouble with its ZipOutputStream compression writing spaces instead of characters. This is the code that I was using:
Code:
        public static bool ConcatLogFolder()
        {
            FastZip z = new FastZip();
            z.CreateZip( "testzip.zip", "Logs", true, "" );

            Crc32 crc = new Crc32();
            ZipOutputStream zip = new ZipOutputStream( File.Create( String.Format( "{0}.zip", GetFileName() ) ) );

            zip.SetLevel( 9 );

            ProcessDirectory( zip, crc, "Logs" );

            zip.Finish();
            zip.Close();

            return true;
        }

        public static void ProcessDirectory( ZipOutputStream zip, Crc32 crc, string path )
        {
            string[] files = Directory.GetFiles( path );
            string[] subDirs = Directory.GetDirectories( path );

            foreach( string filename in files )
                ProcessFile( zip, crc, filename );

            foreach( string dir in subDirs )
                ProcessDirectory( zip, crc, dir );
        }

        public static void ProcessFile( ZipOutputStream zip, Crc32 crc, string path )
        {
            FileStream fs = File.OpenRead( path );
            byte[] buffer = new byte[fs.Length];
            ZipEntry entry = new ZipEntry( ZipEntry.CleanName( path, true ) );

            entry.DateTime = DateTime.Now;
            entry.Size = fs.Length;

            fs.Close();

            crc.Reset();
            crc.Update( buffer );

            entry.Crc = crc.Value;

            zip.PutNextEntry( entry );
            zip.Write( buffer, 0, buffer.Length );
        }
The FastZip works without a problem, but the regular zip method's files are filled with spaces instead of the characters that are really there. The ProcessFile method is almost a clone of the ZipLib's example CreateZip.
__________________
"Misfortune shows those who are not really friends." -Aristotle
"A multitude of words is no proof of a prudent mind." -Thales
Khaz is offline   Reply With Quote
Old 06-14-2006, 07:16 PM   #2 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

It doesn't look like you're actually processing the file at all, just reserving a buffer with the exact size of the file.
Ravatar is offline   Reply With Quote
Old 06-14-2006, 07:32 PM   #3 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default

I found this in the FastZip code:
Code:
		void ProcessFile(object sender, ScanEventArgs e)
		{
			if ( events != null ) {
				events.OnProcessFile(e.Name);
			}
			string cleanedName = nameTransform.TransformFile(e.Name);
			ZipEntry entry = new ZipEntry(cleanedName);
			outputStream.PutNextEntry(entry);
			AddFileContents(e.Name);
		}

		void AddFileContents(string name)
		{
			if ( buffer == null ) {
				buffer = new byte[4096];
			}

			FileStream stream = File.OpenRead(name);
			try {
				int length;
				do {
					length = stream.Read(buffer, 0, buffer.Length);
					outputStream.Write(buffer, 0, length);
				} while ( length > 0 );
			}
			finally {
				stream.Close();
			}
		}
I would have to do something similar to its AddFileContents method, eh? That sounds good and makes sense to me.
__________________
"Misfortune shows those who are not really friends." -Aristotle
"A multitude of words is no proof of a prudent mind." -Thales
Khaz is offline   Reply With Quote
Old 06-14-2006, 07:47 PM   #4 (permalink)
Account Terminated
 
Join Date: Sep 2002
Age: 26
Posts: 3,846
Send a message via ICQ to Phantom Send a message via AIM to Phantom Send a message via MSN to Phantom
Default

I have no idea why I am going to share the following code, but I feel nice, so I suppose it doesn't matter.

Code:
using System;
using System.IO;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Tar;


public class Tar
{
	
	bool keepOldFiles;	
	string archiveName;	
	int blockingFactor;	
	int userId;
	string userName;	
	int groupId;	
	string groupName;	

	public Tar()
	{		
		this.archiveName    = "Test.tar";
		this.keepOldFiles   = false;		
		this.blockingFactor = TarBuffer.DefaultBlockFactor;
		this.userId   = 0;		
		string sysUserName = Environment.UserName;
		this.userName = ((sysUserName == null) ? "" : sysUserName);		
		this.groupId   = 0;
		this.groupName = "None";
	}
	
	public void InstanceMain()
	{
		TarArchive archive = null;					
		Stream outStream = Console.OpenStandardOutput();	
		outStream = File.Create(archiveName);		
		archive = TarArchive.CreateOutputTarArchive(outStream, this.blockingFactor);
		archive.SetKeepOldFiles(this.keepOldFiles);		
		archive.SetUserInfo(this.userId, this.userName, this.groupId, this.groupName);			
		TarEntry entry = TarEntry.CreateEntryFromFile( @"Saves" );		
		archive.WriteEntry( entry, true);													
		archive.CloseArchive();
	}
}
Code:
		private static void GenerateZipFile()
		{
			DirectoryInfo di = new DirectoryInfo("Saves");
			FileSystemInfo[] dirs = di.GetDirectories();
			
			ZipOutputStream s = new ZipOutputStream( File.Create( FileName ) );
			s.SetLevel(9);

			foreach (DirectoryInfo diNext in dirs) 
			{	
				FileInfo[] fi = diNext.GetFiles("*.*");
				foreach (FileInfo fiTemp in fi) 
				{	
					FileStream fs = File.Open( "Saves" + '/' + diNext.Name + '/' + fiTemp.Name,FileMode.Open,FileAccess.Read,FileShare.Read );
            			
					byte[] buffer = new byte[fs.Length];
					fs.Read(buffer, 0, buffer.Length);
            			
					ZipEntry entry = new ZipEntry( diNext.Name + '/' + fiTemp.Name );
            			
					s.PutNextEntry(entry);
            			
					s.Write(buffer, 0, buffer.Length);            					
					fs.Close();
				}
			}		

			s.Finish();
			s.Close();		
			
		}
If you actually release the code, be sure to mention my name, I spent 2 days working on the above code.
Phantom is offline   Reply With Quote
Old 06-14-2006, 07:58 PM   #5 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default

You're sharing it because I have supported and defended you and you love me for that.

Thanks, Phantom. I appreciate it.
__________________
"Misfortune shows those who are not really friends." -Aristotle
"A multitude of words is no proof of a prudent mind." -Thales
Khaz is offline   Reply With Quote
Old 06-14-2006, 07:58 PM   #6 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

Quote:
Originally Posted by Khaz
I found this in the FastZip code:
Code:
		void ProcessFile(object sender, ScanEventArgs e)
		{
			if ( events != null ) {
				events.OnProcessFile(e.Name);
			}
			string cleanedName = nameTransform.TransformFile(e.Name);
			ZipEntry entry = new ZipEntry(cleanedName);
			outputStream.PutNextEntry(entry);
			AddFileContents(e.Name);
		}

		void AddFileContents(string name)
		{
			if ( buffer == null ) {
				buffer = new byte[4096];
			}

			FileStream stream = File.OpenRead(name);
			try {
				int length;
				do {
					length = stream.Read(buffer, 0, buffer.Length);
					outputStream.Write(buffer, 0, length);
				} while ( length > 0 );
			}
			finally {
				stream.Close();
			}
		}
I would have to do something similar to its AddFileContents method, eh? That sounds good and makes sense to me.
That's exactly what you're looking for
Ravatar is offline   Reply With Quote
Old 06-14-2006, 07:59 PM   #7 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default

Thanks for the help!
__________________
"Misfortune shows those who are not really friends." -Aristotle
"A multitude of words is no proof of a prudent mind." -Thales
Khaz is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5