private static int SampleRatio(string filename) { var buffer = new byte[BufferSize]; //TODO: parametrize -- 64KB for now var fsIn = File.OpenRead(filename); string tempFile; do { tempFile = Path.Combine(Environment.GetEnvironmentVariable("temp") ?? Environment.CurrentDirectory, Guid.NewGuid().ToString()); } while (File.Exists(tempFile)); var fsOut = File.Create(tempFile); var offset = fsIn.Length / 2 / _clusterSize * _clusterSize; fsIn.Seek(offset, SeekOrigin.Begin); fsIn.Read(buffer, 0, buffer.Length); fsOut.Write(buffer, 0, buffer.Length); fsIn.Close(); fsOut.Close(); NTFSCompression.Compress(tempFile); var cSize = Allocated(NTFSCompression.FileSize(tempFile)); try { File.Delete(tempFile); } catch { //log? } return((int)cSize / BufferSize * 100); }
private void Compress(string filename) { var action = CompressAction.None; try { var uSize = Allocated(new FileInfo(filename).Length); long cSize; var isCompressed = NTFSCompression.IsCompressed(filename); if (!isCompressed) { if (uSize > _clusterSize) { cSize = SampleRatio(filename); if (uSize < 3 * BufferSize || cSize < DesiredCompression) { NTFSCompression.Compress(filename); action = CompressAction.Compress; isCompressed = true; } else { action = CompressAction.Skip; } } } cSize = Allocated(NTFSCompression.FileSize(filename)); if (cSize >= uSize && isCompressed) { NTFSCompression.Uncompress(filename); _blacklist.Add(filename, filename); action = action == CompressAction.Compress ? CompressAction.None : CompressAction.Decompress; } _actionCount[(int)action]++; if (action == CompressAction.None) { return; } if (uSize > cSize) { _reclaimed += uSize - cSize; } var s = new StringBuilder(); foreach (var a in Enum.GetValues(typeof(CompressAction)).Cast <CompressAction>()) { s.AppendFormat("{0} {1}{2}", a, _actionCount[(int)a], Environment.NewLine); } s.AppendFormat("{0:#,##0} bytes reclaimed.{1}{1}", _reclaimed, Environment.NewLine); s.AppendFormat("{0}", filename); worker.ReportProgress(0, s.ToString()); } catch { //log? } }
private void SaveBlackList() { var writer = File.CreateText(BlackListFile); var array = new string[_blacklist.Count]; _blacklist.Values.CopyTo(array, 0); Array.Sort(array); foreach (var str in array) { writer.WriteLine(str); } writer.Close(); NTFSCompression.Compress(BlackListFile); }
private void startButton_Click(object sender, EventArgs e) { var path = textBox1.Text; startButton.Enabled = false; pauseButton.Enabled = true; stopButton.Enabled = true; _mask = txtMask.Text.Length != 0 ? txtMask.Text : "*"; _clusterSize = NTFSCompression.ClusterSize(Path.GetPathRoot(path)); LoadBlackList(); foreach (CompressAction action in Enum.GetValues(typeof(CompressAction))) { _actionCount[(int)action] = 0; } _reclaimed = 0; worker.RunWorkerAsync(path); statusLabel.Text = "Running"; }