/// <summary> /// Created a new Zip file with the given name. /// </summary> /// <param name="method"> Gzip or deflate</param> /// <param name="name"> Zip name</param> public XyzFile() { try { //zipName = name; //baseStream = new FileStream(zipName, mode); //thisWriter = new XyzEntryWriter(baseStream); //thisWriter.Method = method; //New File //thisWriter.WriteSuperHeader(0, method); //int index1 = zipName.IndexOf(XyzConstants.Dot); //int index2 = zipName.LastIndexOf(XyzConstants.BackSlash); //thisReader = new XyzEntryReader(baseStream, zipName.Substring(index2, // index1 - index2)); //zipEntries = thisReader.GetAllEntries(); //CompressionForm.statusMessage = // String.Format( // System.Threading.Thread.CurrentThread.CurrentUICulture, // XyzConstants.NewMessage, name); } catch (System.IO.IOException) { XyzConstants.ShowError(XyzConstants.IOError); } }
/// <summary> /// Opens a Zip file with the given name. /// </summary> /// <param name="name"> Zip name</param> public XyzFile(string name) { try { zipName = name; baseStream = new FileStream(zipName, FileMode.Open); thisWriter = new XyzEntryWriter(baseStream); int index1 = zipName.IndexOf(XyzConstants.Dot); int index2 = zipName.LastIndexOf(XyzConstants.BackSlash); //thisReader = new XyzEntryReader(baseStream, zipName.Substring(index2, // index1 - index2)); //zipEntries = thisReader.GetAllEntries(); //thisWriter.Method = thisReader.Method; //if (CompressionForm.statusMessage != String.Empty) // CompressionForm.statusMessage = // String.Format( // System.Threading.Thread.CurrentThread.CurrentUICulture, // XyzConstants.OpenMessage, name); } catch (IOException) { XyzConstants.ShowError("Error opening the file"); } catch (ArgumentOutOfRangeException) { XyzConstants.ShowError(XyzConstants.CorruptedError); } }
/// <summary> /// Remove an entry from the archive /// </summary> /// <param name="jump"> /// The offset of the file to be removed /// </param> public void Remove(long jump, XyzEntry entry) { try { //long fileJump = XyzConstants.FixedHeaderSize + // entry.NameLength // +entry.CompressedLength; //baseStream.Seek( jump + fileJump, SeekOrigin.Begin ); //long length = baseStream.Length - fileJump - jump; //byte[] b = new byte[length]; //baseStream.Read( b, 0, (int)length ); //baseStream.Seek( jump, SeekOrigin.Begin ); //baseStream.Write( b, 0, (int)length ); //baseStream.SetLength( baseStream.Length - fileJump ); //CompressionForm.statusMessage = "Removed successfully"; } catch (ArgumentException) { XyzConstants.ShowError(XyzConstants.FileError); } catch (IOException) { XyzConstants.ShowError(XyzConstants.IOError); } }
public static XyzHeadReader ReadXyzHead(System.IO.Stream stream) { byte[] byteTag = new byte[3]; // 00 ~ 03: 标识符号 : 是否是XYZ的标识(读取) int iCount = stream.Read(byteTag, 0, byteTag.Length); if (iCount != byteTag.Length) { return(null); } else if (XyzConstants.CheckBytes(byteTag, XyzHeadReader.XYZ_HEAD_TAG) == false) { return(null); } // 03 ~ 04: XYZ文件版本号(读取) int iHeadVer = stream.ReadByte(); if (iHeadVer > (int)XyzHeadVersion.MaxValue) { return(null); } switch (iHeadVer) { case (int)XyzHeadVersion.XyzHeadVer01: return(ReadXyzHeadVer01(stream)); default: return(null); } }
/// <summary> /// Add a new entry to the zip /// </summary> /// <param name="entry"> /// The details about the header of the entry /// </param> public void Add(XyzEntry entry) { FileStream fs = null; try { fs = File.OpenRead(entry.Name); //entry.Size = (Int32)fs.Length; entry.DateTime = File.GetLastWriteTime(entry.Name); PutNextHeader(entry); WriteCompressedFile(fs, entry); //CompressionForm.statusMessage = XyzConstants.AddMessage; } catch (ArgumentOutOfRangeException) { XyzConstants.ShowError(XyzConstants.ArgumentError); } catch (ArgumentException) { XyzConstants.ShowError(XyzConstants.FileError); } catch (FileNotFoundException) { XyzConstants.ShowError(XyzConstants.FileNotFoundError); } catch (IOException) { XyzConstants.ShowError(XyzConstants.IOError); } catch (OutOfMemoryException) { XyzConstants.ShowError(XyzConstants.MemoryError); } }
public void Extract(int index, string path) { if (index < 0 || index >= zipEntries.Count) { XyzConstants.ShowError("Argument out of range" + "exception"); return; } //thisReader.Extract(zipEntries[index], GetOffset(index + 1), // path); }
/// <summary> /// Writes the superheader /// Resets the pointer back to the original position /// </summary> /// <param name="number">Number of files in the archive</param> private void WriteSuperHeader(Int16 number) { try{ long pos = baseStream.Position; baseStream.Seek(0, SeekOrigin.Begin); WriteLeInt16(number); baseStream.Seek(pos, SeekOrigin.Begin); } catch (IOException) { XyzConstants.ShowError(XyzConstants.IOError); } catch (ArgumentException) { XyzConstants.ShowError(XyzConstants.SeekError); } }
/// <summary> /// Writes the compressed data into the basestream /// It instantiates a memory stream which will serve /// as a temp store and then compresses it using Gzip Stream or /// Deflate stream and writes it to the base stream /// </summary> private void WriteCompressedFile(FileStream fStream, XyzEntry entry) { MemoryStream ms = new MemoryStream(); try { if (method == XyzConstants.DEFLATE) { zipStream = new DeflateStream(ms, CompressionMode.Compress, true); } else if (method == XyzConstants.GZIP) { zipStream = new GZipStream(ms, CompressionMode.Compress, true); } byte[] buffer = new byte[fStream.Length]; fStream.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, buffer.Length); zipStream.Close(); byte[] b = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(b, 0, b.Length); baseStream.Write(b, 0, b.Length); //Go back and write the length and the CRC WriteCompressedLengthCRC((int)ms.Length, ComputeMD5(b), entry); } catch (IOException) { XyzConstants.ShowError(XyzConstants.IOError); } catch (ArgumentException) { XyzConstants.ShowError(XyzConstants.SeekError); } finally { ms.Close(); } }
public void Add(string fileName) { System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture; if (fileName.ToLower(ci).Equals(zipName.ToLower(ci))) { XyzConstants.ShowError("Cannot add the current xip file"); //CompressionForm.statusMessage = String.Empty; return; } XyzEntry entry = new XyzEntry(fileName); thisWriter.Add(entry); //if (CompressionForm.statusMessage.Length != 0) //{ // zipEntries.Add(entry); // thisWriter.CloseHeaders((Int16)zipEntries.Count); //} }
private void WriteCompressedLengthCRC(Int32 value, byte[] crc, XyzEntry entry) { try{ //entry.CompressedLength = value; entry.SetCrc(crc); baseStream.Seek(offset, SeekOrigin.Begin); //WriteLeInt32(entry.CompressedLength); WriteBytes(crc); //Remove the recorded offset offset = -1; baseStream.Seek(0, SeekOrigin.End); } catch (IOException) { XyzConstants.ShowError(XyzConstants.IOError); } catch (ArgumentException) { XyzConstants.ShowError(XyzConstants.ArgumentError); } }
/// <summary> /// Puts the next header in a predefined order /// </summary> /// <param name="entry"> /// the XyzEntry which contains all the information /// </param> private void PutNextHeader(XyzEntry entry) { try{ //WriteLeInt32(entry.Size); ////REcord the offset to write proper CRC and compressed size //offset = baseStream.Position; //WriteLeInt32(entry.CompressedLength); //WriteBytes(entry.GetCrc()); //WriteLeInt32(entry.DosTime); //WriteLeInt16(entry.NameLength); //byte[] names = ConvertToArray(entry.Name); //baseStream.Write(names, 0, names.Length); } catch (IOException) { XyzConstants.ShowError(XyzConstants.IOError); } catch (ArgumentException) { XyzConstants.ShowError(XyzConstants.SeekError); } }