[
  {
    "Id": "653711",
    "ThreadId": "268031",
    "Html": "\r\n<p>I have&nbsp; bunch of text file zipped in zif file.</p>\r\n<p>I wanted to write a routine where every to replace every line starting 8 characters with some input data.I don't wanted to first unzipped copy the file in some folder then change the value and zip it again.Can I change values in the exisiting zipentries\r\n directly?</p>\r\n<p>IF yes then please let me know how?</p>\r\n",
    "PostedDate": "2011-08-06T08:32:49.823-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "653770",
    "ThreadId": "268031",
    "Html": "<p>You can do what you are imagining, without creating filesystem files, <strong>via the ZipInputStream and ZipOutputStream classes</strong>. Instantiate a ZipInputStream on the existing zipfile.&nbsp; Instantiate a ZipOutputStream on a new zipfile.&nbsp; Iteratively call ZipInputStream.GetNextEntry(), which gives you a readable stream.&nbsp; Call ZipOutputstream.PutNextEntry().&nbsp; Read that readable stream, and write the modified output to ZipOutputStream.&nbsp;</p>\n<p>like this:</p>\n<div style=\"background-color: white; color: black;\">\n<pre>    <span style=\"color: blue;\">int</span> n;\r\n    <span style=\"color: blue;\">var</span> buffer = <span style=\"color: blue;\">new</span> <span style=\"color: blue;\">byte</span>[2048];\r\n    <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zis = <span style=\"color: blue;\">new</span> ZipInputStream(originalZipFile))\r\n    {\r\n        <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zos = <span style=\"color: blue;\">new</span> ZipOutputStream(modifiedZipFile))\r\n        {\r\n            ZipEntry e;\r\n            <span style=\"color: blue;\">while</span> ((e = zis.GetNextEntry()) != <span style=\"color: blue;\">null</span>)\r\n            {\r\n                zos.PutNextEntry(e.FileName);\r\n                <span style=\"color: blue;\">bool</span> firstTime = <span style=\"color: blue;\">true</span>;\r\n                <span style=\"color: blue;\">while</span> ((n= zis.Read(buffer,0,buffer.Length)) &gt; 0)\r\n                {\r\n                    <span style=\"color: green;\">// edit the first line only.</span>\r\n                    <span style=\"color: blue;\">if</span> (firstTime)\r\n                    {\r\n                        <span style=\"color: green;\">// implement your edits here.</span>\r\n                        <span style=\"color: green;\">// the following is just an example. </span>\r\n                        buffer[0] = 47;  <span style=\"color: green;\">// slash</span>\r\n                        buffer[1] = 47;  <span style=\"color: green;\">// slash</span>\r\n                        buffer[2] = 32;  <span style=\"color: green;\">// space</span>\r\n                        <span style=\"color: blue;\">for</span> (<span style=\"color: blue;\">int</span> i=3; i &lt; 8; i++)\r\n                            buffer[i] = 65; <span style=\"color: green;\">// 'A'</span>\r\n                        firstTime = <span style=\"color: blue;\">false</span>;\r\n                    }\r\n                    zos.Write(buffer,0,n);\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n</pre>\n</div>\n<p>This does create a 2nd zip file. You will need to delete the original and rename the modified version back to the original name, after completion of this logic.</p>\n<p>&nbsp;</p>",
    "PostedDate": "2011-08-06T10:37:51.417-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "653870",
    "ThreadId": "268031",
    "Html": "<p>I have tried</p>\r\n<p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre>{\r\n            <span style=\"color: blue;\">var</span> objdir = <span style=\"color: blue;\">new</span> DirectoryInfo(strPath);\r\n            Cursor.Current = Cursors.WaitCursor;\r\n            <span style=\"color: blue;\">int</span> i = 0;\r\n            <span style=\"color: blue;\">int</span> n;\r\n            <span style=\"color: blue;\">foreach</span> (<span style=\"color: blue;\">var</span> di <span style=\"color: blue;\">in</span> objdir.GetDirectories())\r\n            {\r\n                <span style=\"color: blue;\">foreach</span> (<span style=\"color: blue;\">var</span> fi <span style=\"color: blue;\">in</span> di.GetFiles())\r\n                {\r\n                    <span style=\"color: blue;\">if</span> (fi.Extension != <span style=\"color: blue;\">null</span> &amp; fi.Extension.ToLower().EndsWith(<span style=\"color: #a31515;\">\".zip\"</span>))\r\n                    {\r\n                        <span style=\"color: blue;\">try</span>\r\n                        {\r\n                           \r\n                            <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zis = <span style=\"color: blue;\">new</span> ZipInputStream(fi.FullName))\r\n                            {\r\n                                <span style=\"color: blue;\">var</span> buffer = <span style=\"color: blue;\">new</span> <span style=\"color: blue;\">byte</span>[2048];\r\n                                System.Text.Encoding ascii = System.Text.Encoding.ASCII;\r\n                                Byte[] encodedBytes = ascii.GetBytes(MethodToReturnRandomStringof8Characters());\r\n                                i++;\r\n                               \r\n                                <span style=\"color: blue;\">var</span> temproraryFileName = <span style=\"color: #a31515;\">\"d:\\\\TestFolder\\\\\" + i.ToString() + \"</span>.zip\";\r\n                                <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zos = <span style=\"color: blue;\">new</span> ZipOutputStream(temproraryFileName))\r\n                                {\r\n                                    ZipEntry ze;\r\n                                    <span style=\"color: blue;\">while</span> ((ze = zis.GetNextEntry()) != <span style=\"color: blue;\">null</span>)\r\n                                    {\r\n                                        ze.IsText = <span style=\"color: blue;\">true</span>;\r\n                                        zos.PutNextEntry(ze.FileName);\r\n                                        <span style=\"color: blue;\">while</span> ((n = zis.Read(buffer, 0, buffer.Length)) &gt; 0)\r\n                                        {\r\n\r\n                                            buffer[0] = encodedBytes[0];  \r\n                                            buffer[1] = encodedBytes[1];\r\n                                            buffer[2] = encodedBytes[2]; \r\n                                            buffer[3] = encodedBytes[3]; \r\n                                            buffer[4] = encodedBytes[4];  \r\n                                            buffer[5] = encodedBytes[5];  \r\n                                            buffer[6] = encodedBytes[6];  \r\n                                            buffer[7] = encodedBytes[7];\r\n\r\n                                            <span style=\"color: blue;\">if</span> (ze.FileName.ToLower().EndsWith(<span style=\"color: #a31515;\">\".hdr\"</span>))\r\n                                            {\r\n                                                buffer[17] = encodedBytes[0]; <span style=\"color: green;\">//also edit some other position</span>\r\n                                                buffer[18] = encodedBytes[1];\r\n                                                buffer[19] = encodedBytes[2];\r\n                                                buffer[20] = encodedBytes[3];\r\n                                                buffer[21] = encodedBytes[4];\r\n                                                buffer[22] = encodedBytes[5];\r\n                                                buffer[23] = encodedBytes[6];\r\n                                                buffer[24] = encodedBytes[7];\r\n                                            }\r\n                                            <span style=\"color: blue;\">else</span>\r\n                                            {\r\n                                                buffer[17] = buffer[17]; <span style=\"color: green;\">//same value</span>\r\n                                                buffer[18] = buffer[18]; \r\n                                                buffer[19] = buffer[19]; \r\n                                                buffer[20] = buffer[20]; \r\n                                                buffer[21] = buffer[21]; \r\n                                                buffer[22] = buffer[22]; \r\n                                                buffer[23] = buffer[23]; \r\n                                                buffer[24] = buffer[24]; \r\n                                            }\r\n                                            zos.Write(buffer, 0, n);\r\n                                        }\r\n                                       \r\n                                    }\r\n                                    zos.Close();\r\n                                    zos.Dispose();\r\n                                    ze = <span style=\"color: blue;\">null</span>;\r\n                                    LogInfo(fi.FullName  + <span style=\"color: #a31515;\">\" is copied to \"</span> + temproraryFileName);\r\n                                }\r\n                                zis.Close();\r\n                                zis.Dispose();\r\n                                buffer = <span style=\"color: blue;\">null</span>;\r\n                            }\r\n                        }\r\n                        <span style=\"color: blue;\">catch</span> (Exception ex)\r\n                        {\r\n                            LogInfo(ex.Message);\r\n                            <span style=\"color: blue;\">continue</span>;\r\n\r\n                        }\r\n                    }\r\n\r\n                }\r\n            }\r\n            LogInfo(i.ToString() + <span style=\"color: #a31515;\">\"files Scanned\"</span>);\r\n            Cursor.Current = Cursors.Default;\r\n        }\r\n</pre>\r\n</div>\r\n</p>\r\n<p>but it didn't worked.What I am seeing is the</p>",
    "PostedDate": "2011-08-06T15:58:07.303-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "653871",
    "ThreadId": "268031",
    "Html": "<p>That's wrong - you're changing 8 bytes in every 2048 bytes.&nbsp; I think you want only the first 8 bytes. Review your code closely, you'll see it.</p>\r\n<p>Also you do not need to Close and Dispose the ZipInputStream and ZipOutputStream if you use a using clause.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2011-08-06T16:05:55.897-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "653877",
    "ThreadId": "268031",
    "Html": "<p>Thanks Cheeso.I know about the Close and dispose doesn't needed in using block but due to general practice I have put that code which will not effect any thing.</p>\n<p>Cheezo can you please suggest what will i need to change?</p>\n<p>I think <span style=\"color: blue;\">var</span> buffer = <span style=\"color: blue;\"> new</span> <span style=\"color: blue;\">byte</span>[8];</p>\n<p>will work but it isn't it</p>",
    "PostedDate": "2011-08-06T16:27:05.657-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "653882",
    "ThreadId": "268031",
    "Html": "<p>Excuse me for being so direct, but:&nbsp; In general practice it is bad - harmful - to include useless, purposeless code in any program.&nbsp;</p>\r\n<p>in your code, you are iterating over all entries in the zip file.&nbsp; For each entry you are reading data, in chunks of 2048 bytes,.&nbsp; You read a chunk, then write it, then read, and write, until there is no more to read. If the file is 4096 bytes long, you read exactly 2 chunks.&nbsp; If the file is 10000 bytes, you read and write 4 complete chunks, and one partial. <em>And for the first chunk only</em>, <strong>you told me </strong>you want to modify 8 bytes or maybe 16 bytes, or whatever. That is what you told me, but <em>in your code</em>, you are not modifying those bytes for the first chunk only.&nbsp; You are modifying the first 8 bytes (and some other bytes) of every chunk of 2048.&nbsp; That means for a file of 10,000 bytes, you introduce modifications into the file 5 times.</p>\r\n<p>You want to modify only the first 8 bytes in the file, you told me.&nbsp;That means you need to make that modification only in the first chunk for each file. &nbsp;Look at the example I provided earlier, it does the right thing.&nbsp; Look at the website. <a href=\"http://dotnetzip.codeplex.com/discussions/268031\">http://dotnetzip.codeplex.com/discussions/268031</a>&nbsp; The code I provided is correct.&nbsp; Look closely.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2011-08-06T16:58:28.283-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "653972",
    "ThreadId": "268031",
    "Html": "<p>Yep I agreed about YAGNI. But due to general practice where i haven't used using block i used to put those close and dispose that's why I have left there.</p>\r\n<p>About my requirement i simply needed to replace starting 8 characters of every line in every zipentry.I have stated about it in my initial post <span style=\"color: #800000;\"><strong>\"</strong>replace <em><strong>every line starting 8 characters</strong></em> with some input data<strong>\".</strong></span> Other then that in a file of particular extension (.hdr) i have to replace 8 characters in every line's position 17-24 as well.</p>",
    "PostedDate": "2011-08-07T02:33:01.647-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654063",
    "ThreadId": "268031",
    "Html": "<p>Ah, I missed that . you will need to read line by line then. The StreamReader does that. Wrap it around the ZipInputStream for each entry.</p>",
    "PostedDate": "2011-08-07T07:53:32.563-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654117",
    "ThreadId": "268031",
    "Html": "<p>I know about reading from the StreamReader in Zip entry.But doesn't know how to write in a zip entry</p>\r\n<p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre> <span style=\"color: blue;\">string</span> strJobID =BLJobReplacer.RandomJobIDGenerator(8);\r\n            <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zis = <span style=\"color: blue;\">new</span> ZipInputStream(originalZipFile))\r\n            {\r\n                <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zos = <span style=\"color: blue;\">new</span> ZipOutputStream(destinationfilename))\r\n                {\r\n                    ZipEntry ze;\r\n                    <span style=\"color: blue;\">while</span> ((ze = zis.GetNextEntry()) != <span style=\"color: blue;\">null</span>)\r\n                    {\r\n                        zos.PutNextEntry (ze.FileName);\r\n                        <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> buffer = <span style=\"color: blue;\">new</span> BufferedStream(ze.OpenReader()))\r\n                        {\r\n                            <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> reader = <span style=\"color: blue;\">new</span> StreamReader((buffer), Encoding.ASCII))\r\n                            {\r\n                                <span style=\"color: blue;\">string</span> line;\r\n                                <span style=\"color: blue;\">while</span> ((line = reader.ReadLine()) != <span style=\"color: blue;\">null</span>)\r\n                                {\r\n                                    <span style=\"color: blue;\">if</span> (line.Trim().Length &gt; 0)\r\n                                    {\r\n                                       <span style=\"color: green;\">// modify line and write it intothe existing one</span>\r\n                                        <span style=\"color: green;\">//strJobID + line.substring(8, line.length -8));         </span>\r\n                                    }\r\n                                }\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n</pre>\r\n</div>\r\nalso searched for example at <a href=\"http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&amp;referringTitle=Examples\" target=\"_blank\">http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&amp;referringTitle=Examples</a>&nbsp; but not able to get for my purpose</p>",
    "PostedDate": "2011-08-07T10:52:20.047-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654187",
    "ThreadId": "268031",
    "Html": "<p>StreamWriter</p>",
    "PostedDate": "2011-08-07T14:20:16.063-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654295",
    "ThreadId": "268031",
    "Html": "<p>I know about StreamWriter.But doesn't know how to use it in this case.</p>\r\n<p>Have used StreamWriter in another location like</p>\r\n<p>&nbsp;using (StreamWriter sw = File.AppendText(strFileName))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (String str in GetUpdatedString())<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw.WriteLine(str);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw.Close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>\r\n<p>&nbsp;</p>\r\n<p>But how to use it in with  zos.PutNextEntry (ze.FileName)?</p>",
    "PostedDate": "2011-08-07T22:12:40.98-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654302",
    "ThreadId": "268031",
    "Html": "<p>Wrap the streamwriter around the output stream.&nbsp; (StreamWriter has a constructor that accepts a stream.)&nbsp;&nbsp;</p>\r\n<p>also: I believe in this case You cannot use StreamWriter in a using clause.&nbsp; StreamWriter will close the wrapped stream when it calls Dispose(), which happens implicitly upon exit of a using scope. But you don't want to close the ZipOutputStream (the wrapped stream)&nbsp;, until you've done all entries.&nbsp; So do not use StreamWriter in a using clause, in this case.&nbsp;</p>\r\n<p>You also need to be mindful of text encodings.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2011-08-07T22:27:34.51-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654452",
    "ThreadId": "268031",
    "Html": "<p>:) That's what i am not able to done.</p>\r\n<p>zos.PutNextEntry take entry name and doesn't have any method to take Streamwriter to be written</p>",
    "PostedDate": "2011-08-08T05:15:14.383-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654491",
    "ThreadId": "268031",
    "Html": "<p>No, you don't give a StreamWriter to PutNextEntry.&nbsp; You wrap a streamwriter around the stream.</p>\r\n<p>something like this:</p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre><span style=\"color: blue;\">private</span> <span style=\"color: blue;\">void</span> ModifyZip()\r\n{\r\n    <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zis = <span style=\"color: blue;\">new</span> ZipInputStream(originalZipFile))\r\n    {\r\n        <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zos = <span style=\"color: blue;\">new</span> ZipOutputStream(destinationfilename))\r\n        {\r\n            ZipEntry ze;\r\n            <span style=\"color: blue;\">while</span> ((ze = zis.GetNextEntry()) != <span style=\"color: blue;\">null</span>)\r\n            {\r\n                zos.PutNextEntry (ze.FileName);\r\n                <span style=\"color: blue;\">var</span> reader = <span style=\"color: blue;\">new</span> StreamReader(zis, Encoding.ASCII, false);\r\n                <span style=\"color: blue;\">var</span> writer = <span style=\"color: blue;\">new</span> StreamWriter(zos, Encoding.ASCII);\r\n                <span style=\"color: blue;\">string</span> line;\r\n                <span style=\"color: blue;\">while</span> ((line = reader.ReadLine()) != <span style=\"color: blue;\">null</span>)\r\n                {\r\n                    line = line.Trim();\r\n                    <span style=\"color: blue;\">if</span> (line.Length &gt; 1)\r\n                    {\r\n                        ModifyLine(line);\r\n                    }\r\n                    writer.WriteLine(line);\r\n                }</pre>\r\n<pre>                writer.Flush();\r\n            }\r\n        }\r\n    }\r\n}\r\n</pre>\r\n</div>\r\n<p>I haven't tried this; it's just an idea.</p>",
    "PostedDate": "2011-08-08T06:24:44.353-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "654499",
    "ThreadId": "268031",
    "Html": "<p>Thanks a lot cheeso.</p>\n<p>It worked</p>",
    "PostedDate": "2011-08-08T06:37:16.12-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]