Ejemplo n.º 1
0
 /// <summary>
 /// To be added.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="fileData"></param>
 /// <returns></returns>
 /// <exception cref="Java.Util.Zip.ZipException"/>
 /// <exception cref="Java.IO.IOException"/>
 public ZipFileBuilder AddFile(string fileName, byte[] fileData)
 {
     var entry = new Java.Util.Zip.ZipEntry(fileName);
     ZipStream.PutNextEntry(entry);
     ZipStream.Write(fileData);
     ZipStream.CloseEntry();
     return this;
 }
Ejemplo n.º 2
0
        public void Zip(string[] files, string zipFilePath, string comment = null, int bufferSize = 1024, CancellationToken?token = null)
        {
            if (string.IsNullOrEmpty(zipFilePath))
            {
                throw new ArgumentNullException(nameof(zipFilePath));
            }

            if (token == null)
            {
                token = CancellationToken.None;
            }

            using (var fileOutputStream = File.Create(zipFilePath))
            {
                using (var zipOutputStream = new Java.Util.Zip.ZipOutputStream(fileOutputStream))
                {
                    if (!string.IsNullOrEmpty(comment))
                    {
                        zipOutputStream.SetComment(comment);
                    }

                    var buffer = new byte[bufferSize];
                    foreach (var file in files.Where(p => !string.IsNullOrEmpty(p)))
                    {
                        if (token.Value.IsCancellationRequested)
                        {
                            break;
                        }

                        using (var entry = new Java.Util.Zip.ZipEntry(Path.GetFileName(file)))
                        {
                            zipOutputStream.PutNextEntry(entry);

                            using (var readStream = File.OpenRead(file))
                            {
                                using (var bufferedStream = new BufferedStream(readStream))
                                {
                                    int count;
                                    while ((count = bufferedStream.Read(buffer, 0, bufferSize)) > 0)
                                    {
                                        zipOutputStream.Write(buffer, 0, count);
                                    }
                                }
                            }

                            zipOutputStream.CloseEntry();
                        }
                    }

                    zipOutputStream.Close();
                }
            }
        }