Compression of songs implemented #14

This commit is contained in:
amazing-username
2019-04-09 03:50:42 -04:00
parent 2d26d6ecdc
commit a17e892a8b
6 changed files with 97 additions and 77 deletions
+59 -31
View File
@@ -1,20 +1,28 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using SevenZip.Compression.LZMA;
using Ionic.Zip;
using Icarus.Models;
namespace Icarus.Controllers.Utilities
{
public class SongCompression
{
#region Fields
string _compressedSongFilename;
string _tempDirectory;
byte[] _uncompressedSong;
#endregion
#region Propterties
public string CompressedSongFilename
{
get => _compressedSongFilename;
set => _compressedSongFilename = value;
}
#endregion
@@ -22,6 +30,10 @@ namespace Icarus.Controllers.Utilities
public SongCompression()
{
}
public SongCompression(string tempDirectory)
{
_tempDirectory = tempDirectory;
}
public SongCompression(byte[] uncompressedSong)
{
_uncompressedSong = uncompressedSong;
@@ -30,40 +42,40 @@ namespace Icarus.Controllers.Utilities
#region Methods
public string RetrieveCompressesSongPath(Song songDetails)
{
string tmpZipFilePath = _tempDirectory + songDetails.Filename;
try
{
using (ZipFile zip = new ZipFile())
{
zip.AddFile(songDetails.SongPath);
zip.Save(tmpZipFilePath);
}
Console.WriteLine("Successfully compressed");
}
catch (Exception ex)
{
var exMsg = ex.Message;
Console.WriteLine("An error ocurred");
Console.WriteLine(exMsg);
}
if (songDetails.Filename.Contains(".mp3"))
{
_compressedSongFilename = StripMP3Extension(songDetails.Filename);
}
return tmpZipFilePath;
}
// Method not being used
public byte[] CompressedSong(byte[] uncompressedSong)
{
byte[] compressedSong = null;
try
{
/**
MemoryStream output = new MemoryStream();
using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
{
dstream.Write(uncompressedSong, 0, uncompressedSong.Length);
compressedSong = output.ToArray();
}
*/
// TODO: Implement song compression
FileStream sourceFile = File.ReadAllBytes(uncompressedSong);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(uncompressedSong, 0, uncompressedSong.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
sourceFile.Close();
destinationFile.Close();
Console.WriteLine("Song has been successfully compressed");
}
catch (Exception ex)
@@ -75,6 +87,22 @@ namespace Icarus.Controllers.Utilities
return compressedSong;
}
string StripMP3Extension(string filename)
{
Console.WriteLine($"Before: {filename}");
int filenameLength = filename.Length;
Console.WriteLine($"Filename length {filenameLength}");
var endIndex = filenameLength - 1;
var startIndex = endIndex - 3;
Console.WriteLine($"Starting index {startIndex} and ending index {endIndex}");
var stripped = filename.Remove(startIndex, 4);
stripped += ".zip";
Console.WriteLine($"After {stripped}");
return stripped;
}
#endregion
}
}