public static ArchiveExtractResult ExtractZip(SharpCompress.Archives.Zip.ZipArchive extractor, string outputFolder, System.EventHandler <ExtractProgress> progress_callback) { Dictionary <bool, List <SharpCompress.Common.IEntry> > myList = new Dictionary <bool, List <SharpCompress.Common.IEntry> >(); myList.Add(true, new List <SharpCompress.Common.IEntry>()); myList.Add(false, new List <SharpCompress.Common.IEntry>()); int total = extractor.Entries.Count; int extractedindex = 0; using (var entries = extractor.ExtractAllEntries()) while (entries.MoveToNextEntry()) { try { FileInfo fi = new FileInfo(Path.Combine(outputFolder, entries.Entry.Key)); FileSystem.CreateDirectory(fi.DirectoryName); using (FileStream fs = fi.Create()) { entries.WriteEntryTo(fs); fs.Flush(); } myList[true].Add(entries.Entry); } catch (System.Exception) { myList[false].Add(entries.Entry); } extractedindex++; if (progress_callback != null) { syncContext.Post(new System.Threading.SendOrPostCallback(delegate { progress_callback?.Invoke(extractor, new ExtractProgress(total, extractedindex)); }), null); } } return(new ArchiveExtractResult(myList)); }
protected void Process(object _) { try { string ufile = System.IO.Path.Combine(_sys, "AutoUpdate.zip"); if (!System.IO.File.Exists(ufile)) { MessageLaunchAndExit("Couldn't find update file."); return; } byte[] buffer = new byte[0x10000]; using (SharpCompress.Archives.Zip.ZipArchive zip = SharpCompress.Archives.Zip.ZipArchive.Open(ufile)) { foreach (var entry in zip.Entries.Where(e => !e.IsDirectory)) { using (var s = entry.OpenEntryStream()) { Status("Updating " + entry.Key); foreach (int i in Enumerable.Range(0, 5)) { try { using (var fs = new System.IO.FileStream(System.IO.Path.Combine(_7h, entry.Key), System.IO.FileMode.Create)) { int len; while ((len = s.Read(buffer, 0, buffer.Length)) != 0) { fs.Write(buffer, 0, len); } } break; } catch (System.IO.IOException) { if (i < 4) { Status(entry.Key + " ...file in use; waiting."); System.Threading.Thread.Sleep(1000); continue; } } if (i == 4) { MessageLaunchAndExit("Update incomplete - could not update all files"); } } } } } Status("Update complete, removing download"); System.IO.File.Delete(ufile); MessageLaunchAndExit("Update complete!"); } catch (Exception ex) { MessageLaunchAndExit("Error applying update: " + ex.ToString()); } }
/// <summary> /// Exports a <see cref="BeatmapSetInfo"/> to an .osz package. /// </summary> /// <param name="set">The <see cref="BeatmapSetInfo"/> to export.</param> public void Export(BeatmapSetInfo set) { var localSet = QueryBeatmapSet(s => s.ID == set.ID); using (var archive = ZipArchive.Create()) { foreach (var file in localSet.Files) { archive.AddEntry(file.Filename, Files.Storage.GetStream(file.FileInfo.StoragePath)); } using (var outputStream = exportStorage.GetStream($"{set}.osz", FileAccess.Write, FileMode.Create)) archive.SaveTo(outputStream); exportStorage.OpenInNativeExplorer(); } }
/// <summary> /// Generate a list of empty folders in an archive /// </summary> /// <param name="input">Input file to get data from</param> /// <returns>List of empty folders in the archive</returns> public override List <string> GetEmptyFolders() { List <string> empties = new List <string>(); try { SharpCompress.Archives.Zip.ZipArchive za = SharpCompress.Archives.Zip.ZipArchive.Open(_filename, new ReaderOptions { LeaveStreamOpen = false }); List <SharpCompress.Archives.Zip.ZipArchiveEntry> zipEntries = za.Entries.OrderBy(e => e.Key, new NaturalSort.NaturalReversedComparer()).ToList(); string lastZipEntry = null; foreach (SharpCompress.Archives.Zip.ZipArchiveEntry entry in zipEntries) { if (entry != null) { // If the current is a superset of last, we skip it if (lastZipEntry != null && lastZipEntry.StartsWith(entry.Key)) { // No-op } // If the entry is a directory, we add it else { if (entry.IsDirectory) { empties.Add(entry.Key); } lastZipEntry = entry.Key; } } } } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); } return(empties); }
public Tuple <List <string>, string> Unzip(string location, string temp) { try { using (SharpCompress.Archives.Zip.ZipArchive archive = SharpCompress.Archives.Zip.ZipArchive.Open(location)) { string specific = ""; List <string> filestodelete = new List <string>(); foreach (var entry in archive.Entries) { entry.WriteToFile(Path.Combine(temp + "\\", entry.Key), new ExtractionOptions() { Overwrite = true, ExtractFullPath = true }); if (entry.Key.ToUpper().Contains("EXE")) { if (Path.GetFileNameWithoutExtension(entry.Key) == Path.GetFileNameWithoutExtension(location)) { continue; } specific = entry.Key; } else { filestodelete.Add(entry.Key); } } return(new Tuple <List <string>, string>(filestodelete, specific)); } } catch { return(new Tuple <List <string>, string>(null, null)); } }