diff --git a/Apkifier.cs b/Apkifier.cs index 9be5e9e..1d9c1e9 100644 --- a/Apkifier.cs +++ b/Apkifier.cs @@ -25,7 +25,7 @@ namespace Emulamer.Utils { - public class Apkifier : IDisposable + public class Apkifier : IDisposable, IApkFileIO { private SHA1 _sha = SHA1Managed.Create(); private UTF8Encoding _encoding = new UTF8Encoding(false); @@ -53,7 +53,11 @@ public string PemData return _pemData; } } - + public bool IsReadOnly + { + get + { return _readOnly; } + } /// /// Creates a new instance of Apkifier @@ -66,7 +70,7 @@ public Apkifier(string filename, bool sign = true, string pemCertificateData = n _filename = filename; _sign = sign; _readOnly = readOnly; - + if (pemCertificateData != null) { _pemData = pemCertificateData; @@ -82,9 +86,10 @@ public Apkifier(string filename, bool sign = true, string pemCertificateData = n _archive = ZipFile.OpenRead(_filename); else _archive = ZipFile.Open(_filename, ZipArchiveMode.Update); + } - + public void LoadCert(byte[] certData) { @@ -139,10 +144,17 @@ public virtual void Write(Stream fileData, string targetPath, bool overwrite = f public virtual void Write(string inputFileName, string targetPath, bool overwrite = false, bool compress = true) { _hasChanges = true; - using (FileStream fs = File.Open(inputFileName, FileMode.Open, FileAccess.Read)) + if (overwrite ) { - Write(fs, targetPath, overwrite, compress); - } + var entry = _archive.GetEntry(targetPath); + if (entry != null) + entry.Delete(); + } + _archive.CreateEntryFromFile(inputFileName, targetPath, compress ? CompressionLevel.Optimal : CompressionLevel.NoCompression); + //using (FileStream fs = File.Open(inputFileName, FileMode.Open, FileAccess.Read)) + //{ + // Write(fs, targetPath, overwrite, compress); + //} } /// @@ -287,7 +299,7 @@ public void Sign() //write the SF to memory then copy it out to the actual file- contents will be needed later to use for signing, don't want to hit the zip stream twice - + byte[] sigFileBytes = null; using (StreamWriter swSignatureFile = GetSW(msSigFile)) @@ -320,10 +332,10 @@ public void Sign() x.Delete(); }); } - + //write the 3 files msManifestFile.Seek(0, SeekOrigin.Begin); - + var manifestEntry = _archive.CreateEntry("META-INF/MANIFEST.MF"); using (Stream s = manifestEntry.Open()) { @@ -385,7 +397,7 @@ private void WriteEntryHashes(ZipArchiveEntry sourceFile, Stream manifestFileStr { swSFFile.WriteLine($"Name: {sourceFile.FullName}"); swSFFile.WriteLine($"SHA1-Digest: {hashOfMFSection}"); - swSFFile.WriteLine(); + swSFFile.WriteLine(); } msSection.Seek(0, SeekOrigin.Begin); @@ -422,7 +434,7 @@ public string GenerateNewCertificatePEM() using (var writer = new StringWriter()) { var pemWriter = new OpenSsl.PemWriter(writer); - + pemWriter.WriteObject(new PemObject("CERTIFICATE", cert.GetEncoded())); pemWriter.WriteObject(subjectKeyPair.Private); return writer.ToString(); @@ -465,7 +477,7 @@ private static X509Certificate LoadCert(string pemData, out AsymmetricKeyParamet private byte[] SignIt(byte[] sfFileData) { AsymmetricKeyParameter privateKey = null; - + var cert = LoadCert(_pemData, out privateKey); //create things needed to make the CmsSignedDataGenerator work diff --git a/IApkFileIO.cs b/IApkFileIO.cs new file mode 100644 index 0000000..9cb1bdf --- /dev/null +++ b/IApkFileIO.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Emulamer.Utils +{ + public interface IApkFileIO : IDisposable + { + bool HasChanges { get; } + bool IsReadOnly { get; } + + void CopyFileInto(string sourceFilePath, string destEntryPath); + void Delete(string pattern); + void Dispose(); + bool FileExists(string targetPath); + IEnumerable FindFiles(string pattern); + string GenerateNewCertificatePEM(); + long GetFileSize(string filename); + Stream GetWriteStream(string targetPath, bool overwrite = false, bool compress = true); + void LoadCert(byte[] certData); + byte[] Read(string targetPath); + void Sign(); + void Write(Stream fileData, string targetPath, bool overwrite = false, bool compress = true); + void Write(string inputFileName, string targetPath, bool overwrite = false, bool compress = true); + } +} \ No newline at end of file