|
We have written a managed zip file component which we are deploying in a code elevate tool. We can create a file stream, write the zip file to disk, and it is ready to send to the receiving server. However, once the move is complete all we do is delete the file. It seems that it would be more valuable to just keep it all in memory and send the memory object to the receiving server.
The first go-around our file stream works great, fast, and gives us a zip file we can open.
Moving the stream to a MemoryStream, we get a larger file than what we had with our first test, and it is an invalid zip file which makes it real hard to determine what is going wrong.
Has anyone done a movement from file to memory with success and what can you suggest for a guidline to make this work.
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
This is what have used with success:
<font size=2 face="Courier New"><font color="#FFFFFF"> </font><font color="#008040">
</font><font color="#000000">FileStream stream </font>= <font color="#000000">File</font>.<font color="#000000">OpenRead</font>(<font color="#000000">filename</font>);
<font color="#000000">byte</font>[] <font color="#000000">buffer </font>= <font color="#0000FF">new </font><font color="#000000">byte</font>[<font color="#000000">stream</font>.<font color="#000000">Length</font>];
<font color="#000000">stream</font>.<font color="#000000">Read</font>(<font color="#000000">buffer</font>, <font color="#FF0000">0</font>, (<font color="#0000FF">int</font>)<font color="#000000">stream</font>.<font color="#000000">Length</font>);
<font color="#000000">stream</font>.<font color="#000000">Close</font>();
<font color="#0000FF">#if(DEBUG)
</font><font color="#008040">
</font><font color="#000000">stream </font>= <font color="#000000">File</font>.<font color="#000000">OpenRead</font>(<font color="#000000">filename</font>.<font color="#000000">Replace</font>(<font color="#FF8000">".dll"</font>, <font color="#FF8000">".pdb"</font>));
<font color="#000000">byte</font>[] <font color="#000000">debugbuffer </font>= <font color="#0000FF">new </font><font color="#000000">byte</font>[<font color="#000000">stream</font>.<font color="#000000">Length</font>];
<font color="#000000">stream</font>.<font color="#000000">Read</font>(<font color="#000000">debugbuffer</font>, <font color="#FF0000">0</font>, (<font color="#0000FF">int</font>)<font color="#000000">stream</font>.<font color="#000000">Length</font>);
<font color="#000000">stream</font>.<font color="#000000">Close</font>();
<font color="#000000">Type</font>[] <font color="#000000">types </font>= <font color="#000000">Assembly</font>.<font color="#000000">Load</font>(<font color="#000000">buffer</font>, <font color="#000000">debugbuffer</font>).<font color="#000000">GetExportedTypes</font>();
<font color="#0000FF">#else
</font><font color="#000000">Type</font>[] <font color="#000000">types </font>= <font color="#000000">Assembly</font>.<font color="#000000">Load</font>(<font color="#000000">buffer</font>).<font color="#000000">GetExportedTypes</font>();
<font color="#0000FF">#endif
</font></font>
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
lippie -- looks like you may have got things reversed. We have working file steam logic we just cannot get the memory stream to work.
Here is what works:
<br />
public static int ZipFilesInto(string zipFileName,string dirPath,string searchPattern,bool recursive, DateTime dateFilter, string basePath)<br />
{<br />
ZipFile zf=new ZipFile(zipFileName,ZipFileOpenMode.Write);<br />
zf.BasePath=basePath;<br />
int ret=zf.Add(dirPath, searchPattern, recursive, dateFilter);<br />
zf.CloseZipFile();<br />
return ret;<br />
}<br />
We are trying to replace this with a memory stream. Here is what does not work.
<br />
public static byte[] ZipFilesToMemory(string dirPath,string searchPattern,bool recursive, DateTime dateFilter, string basePath)<br />
{<br />
MemoryStream m=new MemoryStream();<br />
ZipFile zf=new ZipFile(m,ZipFileOpenMode.Write);<br />
zf.BasePath=basePath;<br />
int ret=zf.Add(dirPath, searchPattern, recursive, dateFilter);<br />
byte[] buf = new byte[m.Length];<br />
int ost=0;<br />
int cnt=0;<br />
while(true)<br />
{<br />
cnt=m.Read(buf,ost,4096);<br />
if(cnt==0)break;<br />
ost+=cnt;<br />
}<br />
zf.CloseZipFile();<br />
return buf;<br />
}<br />
And....here are the methods being called:
<br />
public ZipFile(string zipFilePath,ZipFileOpenMode zom)<br />
{<br />
ZipFilePath=zipFilePath;<br />
OpenZipFile(zom);<br />
}<br />
public ZipFile(Stream baseStream,ZipFileOpenMode zom)<br />
{<br />
if(zom==ZipFileOpenMode.Write)<br />
{<br />
zos = new ZipOutputStream(baseStream);<br />
}<br />
else<br />
{<br />
zis = new ZipInputStream(baseStream);<br />
}<br />
}<br />
public enum ZipFileOpenMode<br />
{<br />
Read,<br />
Write<br />
}<br />
public bool OpenZipFile(ZipFileOpenMode zfom)<br />
{<br />
if(zfom == ZipFileOpenMode.Read)<br />
zis = new ZipInputStream(File.OpenRead(ZipFilePath));<br />
else<br />
zos = new ZipOutputStream(File.Create(ZipFilePath));<br />
<br />
return true;<br />
}<br />
public bool CloseZipFile()<br />
{<br />
if(zis!=null)zis.Close();<br />
if(zos!=null)zos.Close();<br />
zis=null;<br />
zos=null;<br />
return true;<br />
}<br />
It appears the developer is treating both the file stream writer and the memory stream writer as equals, which should be correct. In debug, the data in the memory stream is less than the total capacity allocated yet when he persists it to disk it saves the total capacity size.
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
It seems the Zip library's Stream implementtaion is incorrect. What library are you using? I had problems with the one provided at SharpDevelop's site a few months back, and AFAI can remmeber, it was to do with streams.
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
Ahhhh -- we found the issue. The trouble is that the MemoryStream is not like most streams in that it does not have a reader and a writer. This makes it a bi-directional stream. As such it does not rewind itself for a read after a write. You must specify the position in the stream you want to read from or write to. So we created a read method and a write method to get it to work. Thus the final product is:
<br />
public static byte[] ZipFilesToMemory(string dirPath,string searchPattern,<br />
bool recursive, DateTime dateFilter, string basePath)<br />
{<br />
MemoryStream m=new MemoryStream();<br />
ZipFile zf=new ZipFile(m,ZipFileOpenMode.Write);<br />
zf.BasePath=basePath;<br />
int ret=zf.Add(dirPath, searchPattern, recursive, dateFilter);<br />
zf.ZipOutputStream.Finish();<br />
byte[] buf = new byte[m.Length];<br />
m.Position=0;<br />
m.Read(buf,0,(int)m.Length);<br />
zf.CloseZipFile();<br />
return buf;<br />
}<br />
<br />
public static int ExtractFromMemory(byte[] zipBuffer,string targetPath)<br />
{<br />
MemoryStream m=new MemoryStream();<br />
m.Write(zipBuffer,0,zipBuffer.Length);<br />
ZipFile zf=new ZipFile(m,ZipFileOpenMode.Read);<br />
int cnt=zf.Extract(targetPath);<br />
zf.CloseZipFile();<br />
return cnt;<br />
}<br />
BTW -- we are using the GNU Open Source Zip streams in .NET.
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
Good
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
I have a buuton in my form,when I press this button,another form is open.How can I access controls in first form from second form?
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
You can access controls in the first form if their modifiers value are set to public
No time to breathe
|
|
|
|
|
Really?!!!!!!!!!!!!!!!!!!!!!!!Thats my question.How?!!!!!!!!!I want to do it from second(child) one after it is shown.
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
I'm sorry, my mistakes, I thought u wanna do it from the first form.
But I found a way that u can try, u can access the parent form's controls this way:
Form.ActiveForm.TopLevelControl.Controls[index]
No time to breathe
|
|
|
|
|
Thanks for your help but this return the control from second form,not parent form.
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
I have a problem with datasets. The thing is, that i can not update my dataset. What I have is a dataset with 3 tables which I got from access file with oleDbAdapter. My application changes data in dataset, rows become modified, but when I call update method for the adapter nothing gets updated in source tables. No errors, seems that everything works fine, but it is not so.
oleDbAdapter generates my update/insert/delete commands.
I can not find problem. Can anyone help me?
|
|
|
|
|
I think you need to call AcceptChanges before trying to update.
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
I tried, but still does not work! In this dataset there is a table that i need to update which has 4 fields. In appliction I change all fields, and when I call update method jus two fields are updated, but actally all fields in dataset are changed.
Anyway, AcceptChanges marks all modified rows as unchanged, and these rows update method does not take in count, isn't that?
|
|
|
|
|
Finally i found my problem, i had to use methos EndEdit() before updating dataset!! ))
|
|
|
|
|
Same problem i had over the weekend. I was gonna suggest checking the databinding then Textbox problem?
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
Anyone know of a good C# named pipe example?
Davy
Blog for Software Testing, Bugs, Quality, Security and Stability - www.latedecember.com
News From Angus, Scotland - The Angus Blog
|
|
|
|
|
I want to COPY a directory to another location NOT move it like the code below does.
Does anyone know of such a method call? I searched the help and didn't see anything.
<br />
System.IO.Directory.Move(strSourceName, strDestination); <br />
|
|
|
|
|
MoveFile is in fact only renaming the directory, which is far less involved than duplicating the directory content (including all subdirectories as well).
There is no such Copy thing in the System.IO.Directory class. In fact, the only class that uses Win32Native.CopyFile (a native call, which is what you end up anyway) is the System.IO.File class. Means that you have to enumerate the files, and recursively call this method.
PS : The FCL is not guilty for not providing such method to us since even the native WIN32 ::CopyFile does not duplicate directories.
|
|
|
|
|
Thanks for the comments....BUT isn't it interesting that a user can do a CTRL+C and then a CTRL+V for paste to another directory? Would be nice to be able to do it in code.
|
|
|
|
|
jesus4u wrote:
Would be nice to be able to do it in code.
You are 5 lines of code away from doing it. Please don't tell me it's challenging you. (and writing those lines takes probably even less than the sole time required to post on Cp).
|
|
|
|
|
What do you mean I am 5 lines of code away from doing it?
|
|
|
|
|
A simple question: how do I match # ?
Jonathan de Halleux.
|
|
|
|
|
Jonathan de Halleux wrote:
how do I match # ?
Same as any other non-reserved character, such as letters of the alphabet. The only time it needs to be escaped ("\#") is if you use the RegexOptions.IgnorePatternWhitespace flag.
Cheers
|
|
|
|
|
I want to create an windows form app that loads new forms into an existing area of a form. An example of this is windows explorer.
When you open win explorer, you get 2 pains divided by a splitter bar. If you click on a drive letter a new window opens in the second pained area, and so on...
How do I do this, is there an example that shows how, is there a term that covers this i can search on for examples?
Thanks
uboat42
|
|
|
|