public static void CopyDirectory(string src, string dst) { String[] files; if (dst[dst.Length - 1] != Path.DirectorySeparatorChar) { dst += Path.DirectorySeparatorChar; } if (!SafeDirectory.Exists(dst)) { SafeDirectory.CreateDirectory(dst); } files = SafeDirectory.GetFileSystemEntries(src); foreach (string element in files) { // Sub directories if (SafeDirectory.Exists(element)) { CopyDirectory(element, dst + Path.GetFileName(element)); } // Files in directory else { SafeFile.Copy(element, dst + Path.GetFileName(element), true); } } }
public static void CopyDirectory(string Src, string Dst) { String[] Files; if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar) { Dst += Path.DirectorySeparatorChar; } if (!SafeDirectory.Exists(Dst)) { SafeDirectory.CreateDirectory(Dst); } Files = SafeDirectory.GetFileSystemEntries(Src); foreach (string Element in Files) { // Sub directories if (SafeDirectory.Exists(Element)) { CopyDirectory(Element, Dst + Path.GetFileName(Element)); } // Files in directory else { SafeFile.Copy(Element, Dst + Path.GetFileName(Element), true); } } }
public static void ExtractZipFile(string archiveFilenameIn, string password, string outFolder) { ZipFile zf = null; try { FileStream fs = SafeFile.OpenRead(archiveFilenameIn); zf = new ZipFile(fs); if (!String.IsNullOrEmpty(password)) { zf.Password = password; // AES encrypted entries are handled automatically } foreach (ZipEntry zipEntry in zf) { if (!zipEntry.IsFile) { continue; // Ignore directories } String entryFileName = zipEntry.Name; // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName); // Optionally match entrynames against a selection list here to skip as desired. // The unpacked length is available in the zipEntry.Size property. byte[] buffer = new byte[4096]; // 4K is optimum Stream zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. String fullZipToPath = Path.Combine(outFolder, entryFileName); string directoryName = Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { SafeDirectory.CreateDirectory(directoryName); } // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size // of the file, but does not waste memory. // The "using" will close the stream even if an exception occurs. using (FileStream streamWriter = SafeFile.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } finally { if (zf != null) { zf.IsStreamOwner = true; // Makes close also shut the underlying stream zf.Close(); // Ensure we release resources } } }
public static void OpenExplorerWindow(string fileToSelect) { LogUtil.LogEvent("opening explorer window"); if (!SafeFile.Exists(fileToSelect)) { return; } // combine the arguments together // it doesn't matter if there is a space after ',' string argument = string.Format("/select, \"{0}\"", fileToSelect); System.Diagnostics.Process.Start("explorer.exe", argument); }
public static void ExtractGZip(string gzipFileName, string targetDir, string targetName) { byte[] dataBuffer = new byte[4096]; using (System.IO.Stream fs = new FileStream(gzipFileName, FileMode.Open, FileAccess.Read)) { using (GZipInputStream gzipStream = new GZipInputStream(fs)) { // Change this to your needs string fnOut = Path.Combine(targetDir, targetName); using (FileStream fsOut = SafeFile.Create(fnOut)) { StreamUtils.Copy(gzipStream, fsOut, dataBuffer); } } } }
public static void CreateSample(string outPathname, string password, List <String> contents) { FileStream fsOut = SafeFile.Create(outPathname); ZipOutputStream zipStream = new ZipOutputStream(fsOut); zipStream.SetLevel(3); //0-9, 9 being the highest level of compression zipStream.Password = password; // optional. Null is the same as not setting. foreach (string filename in contents) { string entryName = ZipEntry.CleanName(filename); // Removes drive from name and fixes slash direction ZipEntry newEntry = new ZipEntry(entryName); newEntry.DateTime = DateTime.Now; // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256. // newEntry.AESKeySize = 256; // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code, // you need to do one of the following: Specify UseZip64.Off, or set the Size. // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either, // but the zip will be in Zip64 format which not all utilities can understand. // zipStream.UseZip64 = UseZip64.Off; zipStream.UseZip64 = UseZip64.Off; zipStream.PutNextEntry(newEntry); // Zip the file in buffered chunks // the "using" will close the stream even if an exception occurs byte[] buffer = new byte[4096]; using (FileStream streamReader = SafeFile.OpenRead(filename)) { StreamUtils.Copy(streamReader, zipStream, buffer); } zipStream.CloseEntry(); } zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream zipStream.Close(); }