/// <summary> /// Close a failed stream /// </summary> public void CloseFailed() { // If the stream is already closed, return if (_zipOpen == ZipOpenType.Closed) { return; } // If we're open for read, close the underlying stream if (_zipOpen == ZipOpenType.OpenRead) { Dispose(); _zipOpen = ZipOpenType.Closed; return; } // Otherwise, we only have an open for write left _zipstream.Flush(); _zipstream.Close(); _zipstream.Dispose(); // Delete the failed file Utilities.TryDeleteFile(_zipFileInfo.FullName); _zipFileInfo = null; _zipOpen = ZipOpenType.Closed; }
public void ZipFileClose() { switch (ZipOpen) { case ZipOpenType.Closed: return; case ZipOpenType.OpenRead: { if (_zipFs != null) { _zipFs.Close(); _zipFs.Dispose(); } ZipOpen = ZipOpenType.Closed; return; } case ZipOpenType.OpenWrite: { if (_zipFs != null) { _zipFs.Close(); _zipFs.Dispose(); } ZipOpen = ZipOpenType.Closed; return; } } }
public ZipReturn ZipFileOpen(Stream inStream) { ZipFileClose(); ZipStatus = ZipStatus.None; _zipFileInfo = null; _zipFs = inStream; ZipOpen = ZipOpenType.OpenRead; return(ZipFileReadHeaders()); }
public ZipReturn ZipFileOpen(string newFilename, long timestamp = -1, bool readHeaders = true) { ZipFileClose(); ZipStatus = ZipStatus.None; try { if (!RVIO.File.Exists(newFilename)) { ZipFileClose(); return(ZipReturn.ZipErrorFileNotFound); } _zipFileInfo = new FileInfo(newFilename); if (timestamp != -1 && _zipFileInfo.LastWriteTime != timestamp) { ZipFileClose(); return(ZipReturn.ZipErrorTimeStamp); } int errorCode = FileStream.OpenFileRead(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); if (errorCode == 32) { return(ZipReturn.ZipFileLocked); } return(ZipReturn.ZipErrorOpeningFile); } ZipOpen = ZipOpenType.OpenRead; if (!readHeaders) { return(ZipReturn.ZipGood); } return(ZipFileReadHeaders()); } catch (PathTooLongException) { ZipFileClose(); return(ZipReturn.ZipFileNameToLong); } catch (IOException) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } catch (Exception) { ZipFileClose(); return(ZipReturn.ZipErrorReadingFile); } }
public void ZipFileClose() { if (ZipOpen == ZipOpenType.Closed) { return; } if (ZipOpen == ZipOpenType.OpenRead) { if (_zipFs != null) { _zipFs.Close(); _zipFs.Dispose(); } ZipOpen = ZipOpenType.Closed; return; } }
public ZipReturn ZipFileCreate(string newFilename, bool compressOutput) { if (_zipOpen != ZipOpenType.Closed) { return(ZipReturn.ZipFileAlreadyOpen); } DirUtil.CreateDirForFile(newFilename); _zipFileInfo = new IO.FileInfo(newFilename); int errorCode = IO.FileStream.OpenFileWrite(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } _zipOpen = ZipOpenType.OpenWrite; _signatureHeader = new SignatureHeader(); _header = new Header(); BinaryWriter bw = new BinaryWriter(_zipFs); _signatureHeader.Write(bw); _compressed = compressOutput; _unpackedStreamSize = 0; if (_compressed) { LzmaEncoderProperties ep = new LzmaEncoderProperties(true, 1 << 24, 128); _lzmaStream = new LzmaStream(ep, false, _zipFs); _codeMSbytes = _lzmaStream.Properties; _packStreamStart = (ulong)_zipFs.Position; } return(ZipReturn.ZipGood); }
public void ZipFileClose(ICodeProgress p) { if (_zipOpen == ZipOpenType.Closed) { return; } if (_zipOpen == ZipOpenType.OpenRead) { ZipFileCloseReadStream(); if (_zipFs != null) { _zipFs.Close(); _zipFs.Dispose(); } _zipOpen = ZipOpenType.Closed; return; } CloseWriting7Zip(p); _zipFileInfo = new IO.FileInfo(_zipFileInfo.FullName); _zipOpen = ZipOpenType.Closed; }
public ZipReturn ZipFileOpen(string newFilename, long timestamp, bool readHeaders) { ZipFileClose(); _pZipStatus = ZipStatus.None; _zip64 = false; _centerDirStart = 0; _centerDirSize = 0; _zipFileInfo = null; try { if (!IO.File.Exists(newFilename)) { ZipFileClose(); return ZipReturn.ZipErrorFileNotFound; } _zipFileInfo = new IO.FileInfo(newFilename); if (_zipFileInfo.LastWriteTime != timestamp) { ZipFileClose(); return ZipReturn.ZipErrorTimeStamp; } int errorCode = IO.FileStream.OpenFileRead(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); if (errorCode == 32) return ZipReturn.ZipFileLocked; return ZipReturn.ZipErrorOpeningFile; } } catch (PathTooLongException) { ZipFileClose(); return ZipReturn.ZipFileNameToLong; } catch (IOException) { ZipFileClose(); return ZipReturn.ZipErrorOpeningFile; } ZipOpen = ZipOpenType.OpenRead; if (!readHeaders) return ZipReturn.ZipGood; try { ZipReturn zRet = FindEndOfCentralDirSignature(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } long endOfCentralDir = _zipFs.Position; zRet = EndOfCentralDirRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } // check if this is a ZIP64 zip and if it is read the Zip64 End Of Central Dir Info if (_centerDirStart == 0xffffffff || _centerDirSize == 0xffffffff || _localFilesCount == 0xffff) { _zip64 = true; _zipFs.Position = endOfCentralDir - 20; zRet = Zip64EndOfCentralDirectoryLocatorRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } _zipFs.Position = (long)_endOfCenterDir64; zRet = Zip64EndOfCentralDirRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } } bool trrntzip = false; // check if the ZIP has a valid TorrentZip file comment if (_fileComment.Length == 22) { if (GetString(_fileComment).Substring(0, 14) == "TORRENTZIPPED-") { CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true); byte[] buffer = new byte[_centerDirSize]; _zipFs.Position = (long)_centerDirStart; crcCs.Read(buffer, 0, (int)_centerDirSize); crcCs.Flush(); crcCs.Close(); uint r = (uint)crcCs.Crc; crcCs.Dispose(); string tcrc = GetString(_fileComment).Substring(14, 8); string zcrc = r.ToString("X8"); if (String.Compare(tcrc, zcrc, StringComparison.Ordinal) == 0) trrntzip = true; } } // now read the central directory _zipFs.Position = (long)_centerDirStart; _localFiles.Clear(); _localFiles.Capacity = (int)_localFilesCount; for (int i = 0; i < _localFilesCount; i++) { LocalFile lc = new LocalFile(_zipFs); zRet = lc.CenteralDirectoryRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } _zip64 |= lc.Zip64; _localFiles.Add(lc); } for (int i = 0; i < _localFilesCount; i++) { zRet = _localFiles[i].LocalFileHeaderRead(); if (zRet != ZipReturn.ZipGood) { ZipFileClose(); return zRet; } trrntzip &= _localFiles[i].TrrntZip; } // check trrntzip file order if (trrntzip) for (int i = 0; i < _localFilesCount - 1; i++) { if (TrrntZipStringCompare(_localFiles[i].FileName, _localFiles[i + 1].FileName) < 0) continue; trrntzip = false; break; } // check trrntzip directories if (trrntzip) for (int i = 0; i < _localFilesCount - 1; i++) { // see if we found a directory string filename0 = _localFiles[i].FileName; if (filename0.Substring(filename0.Length - 1, 1) != "/") continue; // see if the next file is in that directory string filename1 = _localFiles[i + 1].FileName; if (filename1.Length <= filename0.Length) continue; if (TrrntZipStringCompare(filename0, filename1.Substring(0, filename0.Length)) != 0) continue; // if we found a file in the directory then we do not need the directory entry trrntzip = false; break; } if (trrntzip) _pZipStatus |= ZipStatus.TrrntZip; return ZipReturn.ZipGood; } catch { ZipFileClose(); return ZipReturn.ZipErrorReadingFile; } }
public ZipReturn ZipFileCreate(string newFilename) { if (ZipOpen != ZipOpenType.Closed) return ZipReturn.ZipFileAlreadyOpen; CreateDirForFile(newFilename); _zipFileInfo = new IO.FileInfo(newFilename); int errorCode = IO.FileStream.OpenFileWrite(newFilename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return ZipReturn.ZipErrorOpeningFile; } ZipOpen = ZipOpenType.OpenWrite; return ZipReturn.ZipGood; }
public void ZipFileCloseFailed() { if (ZipOpen == ZipOpenType.Closed) { return; } if (ZipOpen == ZipOpenType.OpenRead) { if (_zipFs != null) { _zipFs.Close(); _zipFs.Dispose(); } ZipOpen = ZipOpenType.Closed; return; } _zipFs.Flush(); _zipFs.Close(); _zipFs.Dispose(); IO.File.Delete(_zipFileInfo.FullName); _zipFileInfo = null; ZipOpen = ZipOpenType.Closed; }
public void ZipFileClose() { if (ZipOpen == ZipOpenType.Closed) { return; } if (ZipOpen == ZipOpenType.OpenRead) { if (_zipFs != null) { _zipFs.Close(); _zipFs.Dispose(); } ZipOpen = ZipOpenType.Closed; return; } _zip64 = false; bool lTrrntzip = true; _centerDirStart = (ulong)_zipFs.Position; if (_centerDirStart >= 0xffffffff) _zip64 = true; CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true); foreach (LocalFile t in _localFiles) { t.CenteralDirectoryWrite(crcCs); _zip64 |= t.Zip64; lTrrntzip &= t.TrrntZip; } crcCs.Flush(); crcCs.Close(); _centerDirSize = (ulong)_zipFs.Position - _centerDirStart; _fileComment = lTrrntzip ? GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0]; _pZipStatus = lTrrntzip ? ZipStatus.TrrntZip : ZipStatus.None; crcCs.Dispose(); if (_zip64) { _endOfCenterDir64 = (ulong)_zipFs.Position; Zip64EndOfCentralDirWrite(); Zip64EndOfCentralDirectoryLocatorWrite(); } EndOfCentralDirWrite(); _zipFs.SetLength(_zipFs.Position); _zipFs.Flush(); _zipFs.Close(); _zipFs.Dispose(); _zipFileInfo = new IO.FileInfo(_zipFileInfo.FullName); ZipOpen = ZipOpenType.Closed; }
public ZipReturn ZipFileOpen(string filename, long timestamp, bool readHeaders) { Debug.WriteLine(filename); #region open file stream try { if (!IO.File.Exists(filename)) { ZipFileClose(); return(ZipReturn.ZipErrorFileNotFound); } _zipFileInfo = new IO.FileInfo(filename); if (timestamp != -1 && _zipFileInfo.LastWriteTime != timestamp) { ZipFileClose(); return(ZipReturn.ZipErrorTimeStamp); } int errorCode = IO.FileStream.OpenFileRead(filename, out _zipFs); if (errorCode != 0) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } } catch (PathTooLongException) { ZipFileClose(); return(ZipReturn.ZipFileNameToLong); } catch (IOException) { ZipFileClose(); return(ZipReturn.ZipErrorOpeningFile); } #endregion _zipOpen = ZipOpenType.OpenRead; _pZipStatus = ZipStatus.None; try { SignatureHeader signatureHeader = new SignatureHeader(); if (!signatureHeader.Read(new BinaryReader(_zipFs))) { return(ZipReturn.ZipSignatureError); } _baseOffset = _zipFs.Position; //_zipFs.Seek(_baseOffset + (long)signatureHeader.NextHeaderOffset, SeekOrigin.Begin); //byte[] mainHeader = new byte[signatureHeader.NextHeaderSize]; //_zipFs.Read(mainHeader, 0, (int)signatureHeader.NextHeaderSize); //if (!CRC.VerifyDigest(signatureHeader.NextHeaderCRC, mainHeader, 0, (uint)signatureHeader.NextHeaderSize)) // return ZipReturn.Zip64EndOfCentralDirError; _zipFs.Seek(_baseOffset + (long)signatureHeader.NextHeaderOffset, SeekOrigin.Begin); ZipReturn zr = Header.ReadHeaderOrPackedHeader(_zipFs, _baseOffset, out _header); if (zr != ZipReturn.ZipGood) { return(zr); } _zipFs.Seek(_baseOffset + (long)(signatureHeader.NextHeaderOffset + signatureHeader.NextHeaderSize), SeekOrigin.Begin); _pZipStatus = ZipStatus.None; //_pZipStatus = IsRomVault7Z() ? ZipStatus.TrrntZip : ZipStatus.None; //_pZipStatus = Istorrent7Z() ? ZipStatus.TrrntZip : ZipStatus.None; PopulateLocalFiles(out _localFiles); return(ZipReturn.ZipGood); } catch { ZipFileClose(); return(ZipReturn.ZipErrorReadingFile); } }
/// <summary> /// Close the file that the stream refers to /// </summary> public void Close() { // If the stream is already closed, then just return if (_zipOpen == ZipOpenType.Closed) { return; } // If the stream is opened for read, close it if (_zipOpen == ZipOpenType.OpenRead) { Dispose(); _zipOpen = ZipOpenType.Closed; return; } // Now, the only other choice is open for writing so we check everything is correct _zip64 = false; bool torrentZip = true; // Check the central directory _centerDirStart = (ulong)_zipstream.Position; if (_centerDirStart >= 0xffffffff) { _zip64 = true; } // Now loop through and add all of the central directory entries foreach (ZipFileEntry zfe in _entries) { zfe.WriteCentralDirectory(_zipstream); _zip64 |= zfe.Zip64; torrentZip &= zfe.TorrentZip; } _centerDirSize = (ulong)_zipstream.Position - _centerDirStart; // Then get the central directory hash OptimizedCRC ocrc = new OptimizedCRC(); byte[] buffer = new byte[_centerDirSize]; long currentPosition = _zipstream.Position; _zipstream.Position = (long)_centerDirStart; // Then read in the central directory and hash BinaryReader br = new BinaryReader(_zipstream); buffer = br.ReadBytes((int)_centerDirSize); ocrc.Update(buffer, 0, (int)_centerDirSize); string calculatedCrc = ocrc.Value.ToString("X8"); // Finally get back to the original position _zipstream.Position = currentPosition; // Now set more of the information _fileComment = (torrentZip ? Encoding.ASCII.GetBytes(("TORRENTZIPPED-" + calculatedCrc).ToCharArray()) : new byte[0]); _zipStatus = (torrentZip ? ZipStatus.TorrentZip : ZipStatus.None); // If we have a Zip64 archive, write the correct information if (_zip64) { _endOfCenterDir64 = (ulong)_zipstream.Position; WriteZip64EndOfCentralDir(); WriteZip64EndOfCentralDirectoryLocator(); } // Now write out the end of the central directory WriteEndOfCentralDir(); // Finally, close and dispose of the stream _zipstream.SetLength(_zipstream.Position); _zipstream.Flush(); _zipstream.Close(); _zipstream.Dispose(); // Get the new file information _zipFileInfo = new FileInfo(_zipFileInfo.FullName); // And set the stream to closed _zipOpen = ZipOpenType.Closed; }
/// <summary> /// Open a new file as an archive /// </summary> /// <param name="filename">Name of the new file to open</param> /// <param name="timestamp">Timestamp the file should have</param> /// <param name="readHeaders">True if file headers should be read, false otherwise</param> /// <returns>Status of the underlying stream</returns> public ZipReturn Open(string filename, long timestamp, bool readHeaders) { // If a stream already exists, close it Close(); // Now, reset the archive information _zipStatus = ZipStatus.None; _zip64 = false; _centerDirStart = 0; _centerDirSize = 0; _zipFileInfo = null; // Then, attempt to open the file and get information from it try { // If the input file doesn't exist, close the stream and return if (!File.Exists(filename)) { Close(); return(ZipReturn.ZipErrorFileNotFound); } // Get the fileinfo object _zipFileInfo = new FileInfo(filename); // If the timestamps don't match, close the stream and return if (_zipFileInfo.LastWriteTime.Ticks != timestamp) { Close(); return(ZipReturn.ZipErrorTimeStamp); } // Now try to open the file for reading _zipstream = Utilities.TryOpenRead(filename); int read = _zipstream.Read(new byte[1], 0, 1); if (read != 1) { Close(); return(ZipReturn.ZipErrorOpeningFile); } _zipstream.Position = 0; } catch (PathTooLongException) { Close(); return(ZipReturn.ZipFileNameToLong); } catch (IOException) { Close(); return(ZipReturn.ZipErrorOpeningFile); } // If we succeeded, set the flag for read _zipOpen = ZipOpenType.OpenRead; // If we're not reading the headers, return if (!readHeaders) { return(ZipReturn.ZipGood); } //Otherwise, we want to get all of the archive information try { // First, try to get the end of the central directory ZipReturn zr = FindEndOfCentralDirSignature(); if (zr != ZipReturn.ZipGood) { Close(); return(zr); } // Now read the end of the central directory long eocd = _zipstream.Position; zr = ReadEndOfCentralDir(); if (zr != ZipReturn.ZipGood) { Close(); return(zr); } // If we have any indicators of Zip64, check for the Zip64 EOCD if (_centerDirStart == 0xffffffff || _centerDirSize == 0xffffffff || _entriesCount == 0xffff) { _zip64 = true; // Check for the Zip64 EOCD locator _zipstream.Position = eocd - 20; zr = ReadZip64EndOfCentralDirectoryLocator(); if (zr != ZipReturn.ZipGood) { Close(); return(zr); } // If it was found, read the Zip64 EOCD _zipstream.Position = (long)_endOfCenterDir64; zr = ReadZip64EndOfCentralDir(); if (zr != ZipReturn.ZipGood) { Close(); return(zr); } } // Now that we have the rest of the information, check for TorrentZip bool torrentZip = false; if (_fileComment.Length == 22) { if (Encoding.ASCII.GetString(_fileComment).Substring(0, 14) == "TORRENTZIPPED-") { // First get to the right part of the stream OptimizedCRC ocrc = new OptimizedCRC(); byte[] buffer = new byte[_centerDirSize]; _zipstream.Position = (long)_centerDirStart; // Then read in the central directory and hash BinaryReader br = new BinaryReader(_zipstream); buffer = br.ReadBytes((int)_centerDirSize); ocrc.Update(buffer, 0, (int)_centerDirSize); string calculatedCrc = ocrc.Value.ToString("X8"); // If the hashes match, then we have a torrentzip file string extractedCrc = Encoding.ASCII.GetString(_fileComment).Substring(14, 8); if (String.Equals(calculatedCrc, extractedCrc, StringComparison.Ordinal)) { torrentZip = true; } } } // With potential torrentzip out of the way, read the central directory _zipstream.Position = (long)_centerDirStart; // Remove any entries already listed in the archive _entries.Clear(); _entries.Capacity = (int)_entriesCount; // Now populate the entries from the central directory for (int i = 0; i < _entriesCount; i++) { ZipFileEntry zfe = new ZipFileEntry(_zipstream); zr = zfe.ReadCentralDirectory(); // If we get any errors, close and return if (zr != ZipReturn.ZipGood) { Close(); return(zr); } // If we have a Zip64 entry, make sure the archive is _zip64 |= zfe.Zip64; // Now add the entry to the archive _entries.Add(zfe); } // Now that the entries are populated, verify against the actual headers for (int i = 0; i < _entriesCount; i++) { zr = _entries[i].ReadHeader(); // If we get any errors, close and return if (zr != ZipReturn.ZipGood) { Close(); return(zr); } // If we have a torrentzipped entry, make sure the archive is torrentZip &= _entries[i].TorrentZip; } // If we have a torrentzipped file, check the file order if (torrentZip) { for (int i = 0; i < _entriesCount - 1; i++) { if (TorrentZipStringCompare(_entries[i].FileName, _entries[i + 1].FileName) < 0) { continue; } torrentZip = false; break; } } // Now check for torrentzipped directories if we still have a torrentZip file if (torrentZip) { for (int i = 0; i < _entriesCount - 1; i++) { // See if we found a directory string filename0 = _entries[i].FileName; if (filename0.Substring(filename0.Length - 1, 1) != "/") { continue; } // See if the next file is in that directory string filename1 = _entries[i + 1].FileName; if (filename1.Length <= filename0.Length) { continue; } if (TorrentZipStringCompare(filename0, filename1.Substring(0, filename0.Length)) == 0) { continue; } // If we found a file in the directory, then we don't need the directory entry torrentZip = false; break; } } // If we still have torrentzip, say the archive is too if (torrentZip) { _zipStatus |= ZipStatus.TorrentZip; } return(ZipReturn.ZipGood); } catch { Close(); return(ZipReturn.ZipErrorReadingFile); } }