title: Reduce Quality of Image/Stream Before Saving
body: <p>I'm trying to take an input stream (a zip file of images) and extract each file. But i must reduce the quality of each image before they are saved (if quality &lt; 100). I have tried the following but it never compresses the image:</p>
  
  <pre><code>public void UnZip(Stream inputStream, string destinationPath, int quality = 80) {
      using (var zipStream = new ZipInputStream(inputStream)) {
          ZipEntry entry;
  
          while ((entry = zipStream.GetNextEntry()) != null) {
              var directoryPath = Path.GetDirectoryName(destinationPath + Path.DirectorySeparatorChar + entry.Name);
              var fullPath = directoryPath + Path.DirectorySeparatorChar + Path.GetFileName(entry.Name);
  
              // Create the stream to unzip the file to
              using (var stream = new MemoryStream()) {
                  // Write the zip stream to the stream
                  if (entry.Size != 0) {
                      var size = 2048;
                      var data = new byte[2048];
  
                      while (true) {
                          size = zipStream.Read(data, 0, data.Length);
  
                          if (size &gt; 0)
                              stream.Write(data, 0, size);
                          else
                              break;
                      }
                  }
  
                  // Compress the image and save it to the stream
                  if (quality &lt; 100)
                      using (var image = Image.FromStream(stream)) {
                          var info = ImageCodecInfo.GetImageEncoders();
                          var @params = new EncoderParameters(1);
                          @params.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                          image.Save(stream, info[1], @params);
                      }
                  }
  
                  // Save the stream to disk
                  using (var fs = new FileStream(fullPath, FileMode.Create)) {
                      stream.WriteTo(fs);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I'd appreciate it if someone could show me what i'm doing wrong. Also any advice on tidying it up would be appreciated as the code's grown abit ugly. Thanks</p>