Skip to content

Commit

Permalink
Replacing DotNetZip with ZipStorer library
Browse files Browse the repository at this point in the history
The replacement library uses the internal System.IO.Compression classes
along with custom code for reading and writing the central directory
structure of .zip files. The resulting .zip files created may be slightly
larger than those produced via DotNetZip, but we will sacrifice that small
size difference in exchange for cross-platform usability.
  • Loading branch information
jimevans committed Jul 7, 2015
1 parent 1660076 commit 4680a96
Show file tree
Hide file tree
Showing 10 changed files with 1,605 additions and 33 deletions.
15 changes: 10 additions & 5 deletions dotnet/src/webdriver/Firefox/FirefoxExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
using System.IO;
using System.Text;
using System.Xml;
using Ionic.Zip;
using OpenQA.Selenium.Internal;
using System.IO.Compression;

namespace OpenQA.Selenium.Firefox
{
Expand Down Expand Up @@ -83,10 +83,15 @@ public void Install(string profileDir)
// First, expand the .xpi archive into a temporary location.
Directory.CreateDirectory(tempFileName);
Stream zipFileStream = ResourceUtilities.GetResourceStream(this.extensionFileName, this.extensionResourceId);
using (ZipFile extensionZipFile = ZipFile.Read(zipFileStream))
using (ZipStorer extensionZipFile = ZipStorer.Open(zipFileStream, FileAccess.Read))
{
extensionZipFile.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
extensionZipFile.ExtractAll(tempFileName);
List<ZipStorer.ZipFileEntry> entryList = extensionZipFile.ReadCentralDir();
foreach (ZipStorer.ZipFileEntry entry in entryList)
{
string localFileName = entry.FilenameInZip.Replace('/', Path.DirectorySeparatorChar);
string destinationFile = Path.Combine(tempFileName, localFileName);
extensionZipFile.ExtractFile(entry, destinationFile);
}
}

// Then, copy the contents of the temporarly location into the
Expand All @@ -103,7 +108,7 @@ public void Install(string profileDir)

// By deleting the staging directory, we also delete the temporarily
// expanded extension, which we copied into the profile.
Directory.Delete(stagingDirectoryName, true);
FileUtilities.DeleteDirectory(stagingDirectoryName);
}

private static string ReadIdFromInstallRdf(string root)
Expand Down
28 changes: 19 additions & 9 deletions dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
using System.Text;
using System.Xml;
using System.Xml.XPath;
using Ionic.Zip;
using Newtonsoft.Json;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Remote;
using System.IO.Compression;

namespace OpenQA.Selenium.Firefox
{
Expand Down Expand Up @@ -195,10 +195,15 @@ public static FirefoxProfile FromBase64String(string base64)
byte[] zipContent = Convert.FromBase64String(base64);
using (MemoryStream zipStream = new MemoryStream(zipContent))
{
using (ZipFile profileZipArchive = ZipFile.Read(zipStream))
using (ZipStorer profileZipArchive = ZipStorer.Open(zipStream, FileAccess.Read))
{
profileZipArchive.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
profileZipArchive.ExtractAll(destinationDirectory);
List<ZipStorer.ZipFileEntry> entryList = profileZipArchive.ReadCentralDir();
foreach (ZipStorer.ZipFileEntry entry in entryList)
{
string fileName = entry.FilenameInZip.Replace('/', Path.DirectorySeparatorChar);
string destinationFile = Path.Combine(destinationDirectory, fileName);
profileZipArchive.ExtractFile(entry, destinationFile);
}
}
}

Expand Down Expand Up @@ -333,15 +338,20 @@ public string ToBase64String()
{
string base64zip = string.Empty;
this.WriteToDisk();
using (ZipFile profileZipFile = new ZipFile())

using (MemoryStream profileMemoryStream = new MemoryStream())
{
profileZipFile.AddDirectory(this.profileDir);
using (MemoryStream profileMemoryStream = new MemoryStream())
using (ZipStorer profileZipArchive = ZipStorer.Create(profileMemoryStream, string.Empty))
{
profileZipFile.Save(profileMemoryStream);
base64zip = Convert.ToBase64String(profileMemoryStream.ToArray());
string[] files = Directory.GetFiles(this.profileDir, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
string fileNameInZip = file.Substring(this.profileDir.Length).Replace(Path.DirectorySeparatorChar, '/');
profileZipArchive.AddFile(ZipStorer.Compression.Deflate, file, fileNameInZip, string.Empty);
}
}

base64zip = Convert.ToBase64String(profileMemoryStream.ToArray());
this.Clean();
}

Expand Down
Loading

0 comments on commit 4680a96

Please sign in to comment.