public HttpResponseMessage UploadFile()
        {
            var file = HttpContext.Current.Request.Files.Count > 0 ?
                HttpContext.Current.Request.Files[0] : null;

            var fileName = Path.GetFileName(file.FileName);

            var PathFile = Path.Combine(
                HttpContext.Current.Server.MapPath("~/uploads/tozip"),
                fileName
            );
            file.SaveAs(PathFile);

            var zipPath = Path.Combine(
                HttpContext.Current.Server.MapPath("~/uploads"),
                "archive.zip"
            );
            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }
            ZipManager zipManager = new ZipManager();
            zipManager.CompressToZip(HttpContext.Current.Server.MapPath("~/uploads/tozip"), zipPath);

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            var stream = new FileStream(zipPath, FileMode.Open);
            result.Content = new StreamContent(stream);
            result.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/octet-stream");

            return result;
        }
Example #2
0
        public void CompressFileToZipAndCheckZipFileExists()
        {
            string dirName = "dir";
            string zipPath = "archive.zip";

            Directory.CreateDirectory(dirName);

            ZipManager zipManager = new ZipManager();

            zipManager.CompressToZip(dirName, zipPath);

            Assert.IsTrue(File.Exists(zipPath));

            Directory.Delete(dirName);
            File.Delete(zipPath);
        }
Example #3
0
        public void CompressFileToZipAndCheckContentOfThisArchive()
        {
            string dirName = "dir";
            string filePath = dirName + @"\testfile.txt";
            string zipPath = "archive.zip";

            Directory.CreateDirectory(dirName);

            using (StreamWriter sw = new StreamWriter(filePath))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }
            ZipManager zipManager = new ZipManager();

            zipManager.CompressToZip(dirName, zipPath);

            Assert.IsTrue(File.Exists(zipPath));

            Directory.Delete(dirName);
            File.Delete(zipPath);
        }