|
||
|
|
#1 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Posts: 1,754
|
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 );
}
__________________
"Misfortune shows those who are not really friends." -Aristotle "A multitude of words is no proof of a prudent mind." -Thales |
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Posts: 1,754
|
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();
}
}
![]()
__________________
"Misfortune shows those who are not really friends." -Aristotle "A multitude of words is no proof of a prudent mind." -Thales |
|
|
|
|
|
#4 (permalink) |
|
Account Terminated
|
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();
}
|
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Posts: 1,754
|
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 |
|
|
|
|
|
#6 (permalink) | |
|
Forum Expert
Join Date: Sep 2002
Age: 23
Posts: 1,472
|
Quote:
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|