0

the following code is a function to compress each file to a zip file. Now, i want to be able to make a compress limit.

It means, if a file after compress is 7Mb, and my limit is 5Mb, i will get 2 zip files, one will be a 5Mb and the second file will be 2Mb.

my function:

private void CompressFile(string filePath)
    {
        this.CreateZipFolderPath();

        Crc32 crc32 = new Crc32();
        string fileName = Path.GetFileName(filePath);
        string zippedFilePath = XML_Manager.ZippedPALFilePath + fileName;

        using (ZipOutputStream stream = new ZipOutputStream(File.Create(zippedFilePath.Substring(0, zippedFilePath.LastIndexOf('.')) + ".zip")))
        {
            stream.UseZip64 = UseZip64.Off;
            stream.SetLevel(9);

            byte[] buffer = new byte[4096];
            ZipEntry entry = new ZipEntry(filePath.Substring(filePath.LastIndexOf('\\') + 1));
            entry.DateTime = DateTime.Now;
            stream.PutNextEntry(entry);

            using (FileStream fs = File.OpenRead(filePath))
            {
                int sourceBytes;

                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    stream.Write(buffer, 0, sourceBytes);
                }
                while (sourceBytes > 0);
            }

            stream.Finish();
        }
    }

Winrar allows me to do it, here is an example of what i mean:

Example

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

0 Answers0