RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

#ZipLib

Khaz

Knight
#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.
 

Ravatar

Knight
It doesn't look like you're actually processing the file at all, just reserving a buffer with the exact size of the file.
 

Khaz

Knight
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. :)
 

Phantom

Knight
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.
 

Khaz

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

Thanks, Phantom. I appreciate it.
 

Ravatar

Knight
Khaz said:
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 :)
 
Top