/// <summary> /// Explodes the m_zip file into a newly created (randomly named, in the temporary directory) /// destination directory if this hasn't been done already. /// </summary> /// <remarks> /// <c>m_state</c> affects what this method does, and can be changed by this method. /// <c>m_unzipPath</c> can be changed by this method. /// </remarks> /// <exception cref="ObjectDisposedException">State of the reader is "Closed".</exception> /// <exception cref="InvalidPackageException">There is a problem with the zip package.</exception> /// <returns>Package root path.</returns> private string ExplodeZipIfNeeded() { if (m_state == ZipPackageReaderState.Disposed) { throw new ObjectDisposedException("ZipPackageReader"); } if (m_state != ZipPackageReaderState.Exploded) { // If there is an m_stream but no m_zip, we still need to stream the stream into a temp file. if (m_stream != null && m_zip == null) { using (ImpersonateIdentity id = new ImpersonateIdentity(m_impersonationBehavior)) { m_zip = new FileInfo(Path.GetTempFileName()); using (FileStream fileStream = m_zip.Create()) { // Read from the stream and write to the exploded location using the specified identity. Utilities.CopyStream(m_stream, m_impersonationBehavior, fileStream, m_impersonationBehavior); } } m_mustDeleteFile = true; } using (ImpersonateIdentity id = new ImpersonateIdentity(m_impersonationBehavior)) { // Note that Path.Combine is safe here since Path.GetTempPath() and Path.GetRandomFileName() // are known to be safe. // Just in case Path.GetRandomFileName() returns a filename that already exists, keep trying // until success. bool done = false; while (!done) { m_unzipPath = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())); if (m_unzipPath.Exists) continue; m_unzipPath.Create(); done = true; } // explode the zip file try { throw new Exception("Compression class is not found!"); //Compression.Unzip(m_zip, m_unzipPath); m_state = ZipPackageReaderState.Exploded; } catch (Exception e) { // on Exception, convert into InvalidPackageException. // wrap this message into a descriptive message string message = ""; message = String.Format(CultureInfo.InvariantCulture, ValidatorResources.PackageCouldNotBeOpened, message); throw new InvalidPackageException(message, e); } } } return m_unzipPath.ToString(); }
/// <summary> /// Releases all resources used by this object /// </summary> /// <param name="disposing">True if this method was called from /// <Mth>/System.IDisposable.Dispose</Mth></param> protected override void Dispose(bool disposing) { try { if (m_fsPackageReader != null) { m_fsPackageReader.Dispose(); m_fsPackageReader = null; } if (m_state == ZipPackageReaderState.Disposed) { return; } // If the current state is "exploded", delete the directory, files, and all // subdirectories and files. Note that the only way the current state becomes // "exploded" is with a successful unzip operation. if (m_state == ZipPackageReaderState.Exploded) { if (m_unzipPath != null) { try { m_unzipPath.Delete(true); } catch (IOException) { // file can't be deleted if currently open. Unfortunately this means there will be // files left behind. // noop } } // delete the created file if needed. if (m_mustDeleteFile && m_zip != null) { m_zip.Delete(); } } } finally { m_state = ZipPackageReaderState.Disposed; base.Dispose(disposing); } }