开发者

in C# .net framework 2.0 how can i unzip a .zip file to a specified directory and have it wait untill it is done before moveing on

开发者 https://www.devze.com 2023-01-04 13:47 出处:网络
i have a .zip file that my application needs to extract, and run how would i go about doing this? it needs to extract Programs\\Test\\Build.zip to the directory:

i have a .zip file that my application needs to extract, and run how would i go about doing this? it needs to extract Programs\Test\Build.zip to the directory:

string tempFolder = System.Environment.GetEvironmentVariable("HomeDrive");
开发者_Go百科

then it needs to wait till its finished, then run the exe "Compile.exe" that was extracted, ideas?


Well, for one thing you could extract it in code, using SharpZipLib.

There's a sample for unpacking a complete zip file.

Once that's done, just use Process.Start to launch the executable.


I have used http://dotnetzip.codeplex.com/

Lots of examples here: http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples


//  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vjslib.dll
    // <add assembly="vjslib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    public class ZipWriter : Stream {
        public ZipWriter() { dest = new MemoryStream(); }
        public ZipWriter(Stream owner) { dest = owner; }
        public ZipWriter(Stream owner, string FileName) { dest = owner; AddFile(FileName); }



        public void AddFile(string FileName) {
            if (writer == null) writer = new zipwriter(this);                
            if (writer.az == null) writer.az = new java.util.zip.ZipOutputStream(writer); else writer.az.closeEntry();
            writer.az.putNextEntry(new java.util.zip.ZipEntry(FileName));

        }

        public byte[] ToArray() { finalize(); return ((MemoryStream)dest).ToArray(); }


        public void finalize()
        { /*Закрыть архив*/
            if (writer.az == null) return; writer.az.closeEntry(); writer.az.finish(); writer.az = null;
        }
        public override long Length { get { finalize(); return dest.Length; } }

        /*Забрать zip*/


        public Stream dest;

        class zipwriter : java.io.OutputStream
        {                
            public ZipWriter parent;
            public java.util.zip.ZipOutputStream az;
            public override void write(sbyte[] buffer, int offset, int count){ 
                if (count<=0) return;
                byte[] dest = new byte[offset + count];
                Buffer.BlockCopy(buffer, offset, dest, offset, count);
                parent.dest.Write( dest, offset,count);              
            }
            public override void write(int b) {  parent.dest.WriteByte((byte)b); }
            public zipwriter(ZipWriter owner) { parent = owner; }

        }
        zipwriter writer;
        long      position;
        public override bool CanRead { get {return false; } }            
        public override bool CanSeek { get {return false;} }            
        public override bool CanWrite { get {return true;} }


        public override long Position { get {  return position;  } set { position = value;} }            
        protected override void Dispose(bool disposing){  }
        public override void Flush() { dest.Flush(); }            
        public override int Read(byte[] buffer, int offset, int count){ return 0;}

        public override long Seek(long offset, SeekOrigin origin) {  return position; }

        public override void SetLength(long value) { }

        public override void Write(byte[] buffer, int offset, int count){                                 
            if (count<=0) return;                
             sbyte[] dest = new sbyte[offset + count];
             Buffer.BlockCopy(buffer, offset, dest, offset, count);
             writer.az.write(dest, offset, count);
             position += count;                
           }
       }


       /* Example:
          ZipWriter ZipStream = new ZipWriter();
          ZipStream.AddFile("1.txt");
          ...  ZipStream.WriteByte(65);

         Response.Clear();
         Response.ContentType = "application/force-download";
         Response.AddHeader("content-disposition",   "attachment; filename=1.zip");
         Response.BinaryWrite(ZipStream.ToArray());
         Response.End();          
       */


I suggest using SharpZipLib as described here. It's easy to use and I think it's synchronous by default. See this page for samples on how to use it.

And then just execute it using Process.Start

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号