Example #1
0
        /// <summary>
        ///     Unzips the data from <paramref name="packageData"/> and access <see cref="ModuleManifest"/>.
        /// </summary>
        /// <param name="packageData">The raw data of the package.</param>
        /// <returns><see cref="ModuleInfo"/> object that has ModuleManifest found.</returns>
        public IModuleInfo UnPack(byte[] packageData)
        {
            if (packageData == null)
            {
                throw new ArgumentNullException("packageData");
            }

            ZipFile        zipFile  = null;
            ModuleManifest manifest = null;

            try
            {
                zipFile = ZipFile.Read(packageData);

                // find manifest inside the zip data
                foreach (ZipEntry zipEntry in zipFile)
                {
                    if (zipEntry.FileName.EndsWith(ModuleManifest.ManifestFileNameSuffix))
                    {
                        // use this file as manifest
                        var ms = new MemoryStream();
                        zipEntry.Extract(ms);
                        manifest = XmlSerializerHelper.Deserialize <ModuleManifest>(ms.ToArray());

                        // now search for the assembly described by this manifest, simply by iterating through zip entries
                        if (
                            !zipFile.Any(
                                entry =>
                                entry.FileName.EndsWith(".dll") | entry.FileName.EndsWith("exe")))
                        {
                            throw new ArgumentException("No assembly in the package");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new InvalidDataException("The file is corrupted", e);
            }
            finally
            {
                if (zipFile != null)
                {
                    zipFile.Dispose();
                }
            }

            // have not found manifest
            if (manifest == null)
            {
                throw new InvalidDataException("No manifest file in package");
            }

            return(new ModuleInfo
            {
                ModuleData = packageData,
                Manifest = manifest
            });
        }
Example #2
0
        /// <summary>
        /// Save this PXZ file to disk
        /// </summary>
        /// <param name="path">The file to save to/create</param>
        public void Save(string path)
        {
            //delete the existing file (if it exists)
            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            //new index attributes (don't override records though)
            FileIndex.Author = PxzAuthor.FromCurrent();

            var idxDoc = FileIndex.ToXml();

            //deflate the content to save room (poster isn't compressed via Zlib)
            var idxByte = GZipCompressor.CompressString(idxDoc.OuterXml);

            //names of root format items
            const string idxName = @"index";
            const string recName = @"records";

            //create a new temporary Zip file (in memory, not on disk)
            using var archive = new ZipFile(path, Encoding.Default);

            //add the indexing information to the PXZ
            archive.AddEntry(idxName, idxByte);

            //add records folder
            archive.AddDirectoryByName(recName);

            //traverse the records list
            foreach (var r in Records)
            {
                //null records are skipped
                if (r == null)
                {
                    continue;
                }

                //convert to record string
                var raw = r.ToRawForm();

                //record name
                var fileName = $"{recName}/{r.Header.Naming.StoredName}";

                //add the ZIP entry of the file
                if (!archive.Any(entry => entry.FileName.EndsWith(fileName)))
                {
                    archive.AddEntry(fileName, raw);
                }
            }

            //finalise ZIP file
            archive.Save(path);

            //cull the residual archive from memory
            archive.Dispose();
        }
Example #3
0
        public static void SignReady(string apkPath)
        {
            string zipPath;

            zipPath = Path.ChangeExtension(apkPath, ".zip");
            File.Move(apkPath, zipPath);
            using (ZipFile zipFile = ZipFile.Read(zipPath))
            {
                bool folderExists = zipFile.Any(entry => entry.FileName.Contains("META-INF"));
                if (folderExists)
                {
                    zipFile.RemoveSelectedEntries("META-INF/");
                }
                zipFile.Save();
            }
            apkPath = Path.ChangeExtension(zipPath, ".apk");
            File.Move(zipPath, apkPath);
        }
        private void DownloadZippedFiles(HttpContextBase httpContext, IEnumerable <DownloadItem> items)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext", "Cannot be null.");
            }
            if (items == null)
            {
                throw new ArgumentNullException("items", "Cannot be null.");
            }

            using (ZipFile zf = new ZipFile())
            {
                Dictionary <string, int> duplicateNames = new Dictionary <string, int>();

                foreach (DownloadItem item in items)
                {
                    string fileName = GetFullFileName(item.FileName, item.Extension);

                    // Handle duplicates:
                    if (zf.Any(i => i.FileName == fileName))
                    {
                        if (duplicateNames.ContainsKey(fileName))
                        {
                            duplicateNames[fileName]++;
                        }
                        else
                        {
                            duplicateNames.Add(fileName, 2);
                        }

                        fileName = GetFullFileName(item.FileName + string.Format(" ({0})", duplicateNames[fileName]), item.Extension);
                    }

                    zf.AddEntry(fileName, item.Data);
                }

                httpContext.Response.ContentType = "application/zip";
                httpContext.Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}.zip", _packageFileName));
                zf.Save(httpContext.Response.OutputStream);
            }
        }
        public override ContentDirectory GetDirectory(string relativePath)
        {
            relativePath = relativePath.ToForwardSlashes();

            //this slash will be removed later, but we need it to make sure we are matching mod/ and not mod.txt
            if (!relativePath.EndsWith("/"))
            {
                relativePath = relativePath + "/";
            }

            var sourceRelativePath = Path.Combine(this.SourceRelativePath, relativePath).ToLower();

            var exists = _zip.Any(p => p.FileName.ToLower().StartsWith(sourceRelativePath));

            if (exists)
            {
                return(new ZipFileDirectory(_zip, sourceRelativePath, IsCoreGame));
            }
            else
            {
                return(null);
            }
        }
        /// ------------------------------------------------------------------------------------
        public static string GetProjectNameFromBackupFile(string backupFile)
        {
            if (!ZipFile.IsZipFile(backupFile, true))
            {
                return(null);
            }

            using (var zip = new ZipFile(backupFile))
            {
                if (!zip.Any(ze => ze.FileName == kBackupInfoFileName))
                {
                    return(null);
                }

                zip.ExtractSelectedEntries(kBackupInfoFileName, string.Empty,
                                           Path.GetTempPath(), ExtractExistingFileAction.OverwriteSilently);

                var infoFile = Path.Combine(Path.GetTempPath(), kBackupInfoFileName);
                var root     = XElement.Load(infoFile);
                try { File.Delete(infoFile); }
                catch { }
                return((string)root.Element("project"));
            }
        }
Example #7
0
 public bool IsFileExist(string path)
 {
     return(zip.Any(entry => entry.FileName == path));
 }