public void TestExtractArchiveTarGzCreateContainer()
        {
            CloudFilesProvider provider = (CloudFilesProvider)Bootstrapper.CreateObjectStorageProvider();
            string containerName = TestContainerPrefix + Path.GetRandomFileName();
            string sourceFileName = "DarkKnightRises.jpg";
            byte[] content = File.ReadAllBytes("DarkKnightRises.jpg");
            using (MemoryStream outputStream = new MemoryStream())
            {
                using (GZipOutputStream gzoStream = new GZipOutputStream(outputStream))
                {
                    gzoStream.IsStreamOwner = false;
                    gzoStream.SetLevel(9);
                    using (TarOutputStream tarOutputStream = new TarOutputStream(gzoStream))
                    {
                        tarOutputStream.IsStreamOwner = false;
                        TarEntry entry = TarEntry.CreateTarEntry(containerName + '/' + sourceFileName);
                        entry.Size = content.Length;
                        tarOutputStream.PutNextEntry(entry);
                        tarOutputStream.Write(content, 0, content.Length);
                        tarOutputStream.CloseEntry();
                        tarOutputStream.Close();
                    }
                }

                outputStream.Flush();
                outputStream.Position = 0;
                ExtractArchiveResponse response = provider.ExtractArchive(outputStream, "", ArchiveFormat.TarGz);
                Assert.IsNotNull(response);
                Assert.AreEqual(1, response.CreatedFiles);
                Assert.IsNotNull(response.Errors);
                Assert.AreEqual(0, response.Errors.Count);
            }

            using (MemoryStream downloadStream = new MemoryStream())
            {
                provider.GetObject(containerName, sourceFileName, downloadStream, verifyEtag: true);
                Assert.AreEqual(content.Length, GetContainerObjectSize(provider, containerName, sourceFileName));

                downloadStream.Position = 0;
                byte[] actualData = new byte[downloadStream.Length];
                downloadStream.Read(actualData, 0, actualData.Length);
                Assert.AreEqual(content.Length, actualData.Length);
                using (MD5 md5 = MD5.Create())
                {
                    byte[] contentMd5 = md5.ComputeHash(content);
                    byte[] actualMd5 = md5.ComputeHash(actualData);
                    Assert.AreEqual(BitConverter.ToString(contentMd5), BitConverter.ToString(actualMd5));
                }
            }

            /* Cleanup
             */
            provider.DeleteContainer(containerName, deleteObjects: true);
        }
Пример #2
0
 /// <summary>
 /// Close the archive. This simply calls the underlying
 /// tar stream's close() method.
 /// </summary>
 public void CloseArchive()
 {
     if (tarIn != null)
     {
         tarIn.Close();
     }
     else if (tarOut != null)
     {
         tarOut.Flush();
         tarOut.Close();
     }
 }
Пример #3
0
        public void Checksum()
        {
            MemoryStream ms = new MemoryStream();
            TarOutputStream tarOut = new TarOutputStream(ms);

            DateTime modTime = DateTime.Now;

            TarEntry entry = TarEntry.CreateTarEntry("TestEntry");
            entry.TarHeader.Mode = 12345;

            tarOut.PutNextEntry(entry);
            tarOut.Close();

            MemoryStream ms2 = new MemoryStream();
            ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            ms2.Seek(0, SeekOrigin.Begin);

            TarInputStream tarIn = new TarInputStream(ms2);
            TarEntry nextEntry = tarIn.GetNextEntry();

            Assert.IsTrue(nextEntry.TarHeader.IsChecksumValid, "Checksum should be valid");

            MemoryStream ms3 = new MemoryStream();
            ms3.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            ms3.Seek(0, SeekOrigin.Begin);
            ms3.Write(new byte[1] { 34 }, 0, 1);
            ms3.Seek(0, SeekOrigin.Begin);

            tarIn = new TarInputStream(ms3);
            bool trapped = false;

            try
            {
                nextEntry = tarIn.GetNextEntry();
            }
            catch (TarException)
            {
                trapped = true;
            }

            Assert.IsTrue(trapped, "Checksum should be invalid");
        }
Пример #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (isDisposed)
     {
         return;
     }
     isDisposed = true;
     if (disposing)
     {
         if (tarOut != null)
         {
             tarOut.Flush();
             tarOut.Close();
         }
         if (tarIn != null)
         {
             tarIn.Close();
         }
     }
 }
Пример #5
0
        public void OutputStreamOwnership()
        {
            TrackedMemoryStream memStream = new TrackedMemoryStream();
            TarOutputStream s = new TarOutputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.Close();

            Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
            Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");

            memStream = new TrackedMemoryStream();
            s = new TarOutputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.IsStreamOwner = false;
            s.Close();

            Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
            Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
        }
Пример #6
0
        protected override void _Partir(string fichero, string sal1, string dir, long kb)
        {
            FileInfo fi = null;
            DirectoryInfo din = null;
            DirectoryInfo dout = new DirectoryInfo (dir);

            if (File.Exists (fichero)) {
                fi = new FileInfo (fichero);
            } else if (Directory.Exists (fichero)) {
                din = new DirectoryInfo (fichero);
            } else {
                throw new Exception ("" + fichero + " not found");
            }
            List<FileInfo> files = load (fichero);

            string baseName = "";
            if (fi != null) {
                baseName = fi.Name;
            } else if (din != null) {
                baseName = din.Name;
            }

            if ((sal1 == null) || (sal1 == string.Empty)) {
                //
                if (din != null) {
                    sal1 = din.Name;
                }
                if (fi != null) {
                    sal1 = fi.Name;
                }
            }

            long totalSize = calculateTotalSize (files);
            long fragments = totalSize / (kb * 1024);
            string s = "" + fragments;
            JoinInfo info = new JoinInfo ();
            info.OriginalFile = baseName;
            info.InitialFragment = 0;
            info.Digits = Math.Max (s.Length, 3);
            info.BaseName = sal1 + ".tar.gz.";
            info.Directory = dout;
            info.Length = totalSize;

            Stream stream = new SplitStream (info, kb * 1024, info.Directory.FullName + Path.DirectorySeparatorChar + info.BaseName + "sha512sum.dalle", "SHA512");
            stream = new GZipStream (stream, CompressionMode.Compress);
            TarOutputStream taros = new TarOutputStream (stream);
            foreach (FileInfo f in files) {

                TarEntry te = TarEntry.CreateEntryFromFile (f.FullName);
                te.UserId = 0;
                te.GroupId = 0;
                te.UserName = String.Empty;
                te.GroupName = String.Empty;
                taros.PutNextEntry (te);
                FileStream fs = f.OpenRead ();
                long leidosTotales = 0;
                byte[] buffer = new byte[Consts.BUFFER_LENGTH];
                int leidos = 0;
                while ((leidos = fs.Read (buffer, 0, buffer.Length)) > 0) {
                    taros.Write (buffer, 0, leidos);
                    leidosTotales += leidos;
                    OnProgress (leidosTotales, totalSize);
                }
                taros.CloseEntry ();
                fs.Close ();
            }
            taros.Close ();
            OnProgress (totalSize, totalSize);
        }
Пример #7
0
        void TryLongName(string name)
        {
            MemoryStream ms = new MemoryStream();
            TarOutputStream tarOut = new TarOutputStream(ms);

            DateTime modTime = DateTime.Now;

            TarEntry entry = TarEntry.CreateTarEntry(name);

            tarOut.PutNextEntry(entry);
            tarOut.Close();

            MemoryStream ms2 = new MemoryStream();
            ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            ms2.Seek(0, SeekOrigin.Begin);

            TarInputStream tarIn = new TarInputStream(ms2);
            TarEntry nextEntry = tarIn.GetNextEntry();

            Assert.AreEqual(nextEntry.Name, name, "Name match failure");
        }
Пример #8
0
        public void ValuesPreserved()
        {
            MemoryStream ms = new MemoryStream();
            TarOutputStream tarOut = new TarOutputStream(ms);

            DateTime modTime = DateTime.Now;

            TarEntry entry = TarEntry.CreateTarEntry("TestEntry");
            entry.GroupId = 12;
            entry.UserId = 14;
            entry.ModTime = modTime;
            entry.UserName = "******";
            entry.GroupName = "GroupName";
            entry.TarHeader.Mode = 12345;

            tarOut.PutNextEntry(entry);
            tarOut.Close();

            MemoryStream ms2 = new MemoryStream();
            ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            ms2.Seek(0, SeekOrigin.Begin);

            TarInputStream tarIn = new TarInputStream(ms2);
            TarEntry nextEntry = tarIn.GetNextEntry();
            Assert.AreEqual(entry.TarHeader.Checksum, nextEntry.TarHeader.Checksum, "Checksum");

            Assert.IsTrue(nextEntry.Equals(entry), "Entries should be equal");
            Assert.IsTrue(nextEntry.TarHeader.Equals(entry.TarHeader), "Headers should match");

            // Tar only stores seconds
            DateTime truncatedTime = new DateTime(modTime.Year, modTime.Month, modTime.Day,
                                                  modTime.Hour, modTime.Minute, modTime.Second);
            Assert.AreEqual(truncatedTime, nextEntry.ModTime, "Modtimes should match");

            int entryCount = 0;
            while ( nextEntry != null )
            {
                ++entryCount;
                nextEntry = tarIn.GetNextEntry();
            }

            Assert.AreEqual(1, entryCount, "Expected 1 entry");
        }
Пример #9
0
		override public void AddToStream (Stream stream, EventTracker tracker)
		{
			if (tracker != null)
				tracker.ExpectingAdded (UriFu.UriToEscapedString (this.Uri));

			UnclosableStream unclosable;
			unclosable = new UnclosableStream (stream);

			TarOutputStream tar_out;
			tar_out = new TarOutputStream (unclosable);
			foreach (FileSystemObject fso in Children)
				WriteObjectToTar (tar_out, fso, tracker);
			
			// This calls close on the underlying stream,
			// which is why we wrapped the stream in an
			// UnclosableStream.
			tar_out.Close ();
		}