2
// Copyright (C) 2011 Bartek thindil Jasicki
4
// This file is part of Grubng
6
// Grubng is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.
23
using System.IO.Compression;
28
/// Provide function for manipulate files.
30
internal sealed class Files : IDisposable
33
/// Object for database operations.
37
/// Object for show messages to user.
41
/// If true, all disposable fields are disposed.
46
/// Standard class constructor.
48
/// <param name="quiet">
49
/// A <see cref="System.Boolean"/> if true, don't show messages to user.
51
public Files (bool quiet)
53
this.db = new Database();
54
this.util = new Utils(quiet);
58
/// Function create database backup to selected file.
60
/// <param name="filename">
61
/// A <see cref="System.String"/> name of backup file.
63
public void Backup(string filename)
67
throw new ObjectDisposedException(GetType().Name);
69
this.util.Message("Creating database backup ... ", false);
70
using (StreamWriter writer = new StreamWriter(filename))
72
StringBuilder urls2 = new StringBuilder();
73
this.util.ProgressStart(0, "URL's was saved");
76
urls2.Append(this.db.SelectURLs(this.util.Curamount));
77
if (urls2.Length == 0)
81
urls2.Append(Environment.NewLine);
84
urls2.Remove(0, urls2.Length);
85
this.util.Curamount += 100000;
86
GC.GetTotalMemory(true);
88
this.util.ProgressStop();
91
this.util.Message(" done.", true);
92
this.util.Message("Compressing backup file ... ", false);
93
using (FileStream file = File.OpenRead(filename))
95
using (FileStream workunit = File.Create(filename + ".lzma"))
97
long length = file.Length;
102
object[] properties = { (int)(length),
111
using (Grubng.Encoder encoder = new Grubng.Encoder())
113
CoderPropID[] propIDs = {CoderPropID.DictionarySize,
114
CoderPropID.PosStateBits,
115
CoderPropID.LitContextBits,
116
CoderPropID.LitPosBits,
117
CoderPropID.Algorithm,
118
CoderPropID.NumFastBytes,
119
CoderPropID.MatchFinder,
120
CoderPropID.EndMarker
122
encoder.SetCoderProperties(propIDs, properties);
123
encoder.WriteCoderProperties(workunit);
124
for (int i = 0; i < 8; i++)
126
workunit.WriteByte((Byte)(length >> (8 * i)));
128
encoder.Code(file, workunit);
133
this.util.Message("done", true);
137
/// Function read list of URL's from file and send they to database.
139
/// <param name="filename">
140
/// A <see cref="System.String"/> filename with URL's.
142
/// <param name="fast">
143
/// A <see cref="System.Boolean"/> if true, do fast insert URL's to database.
145
public void AddURLs(string filename, bool fast)
149
throw new ObjectDisposedException(GetType().Name);
151
FileInfo finfo = new FileInfo(filename);
152
if (finfo.Extension == ".gz")
154
this.util.Message("Decompressing file ... ", false);
155
string newfilename = filename.Remove(filename.Length - 3);
156
using (FileStream file2 = File.OpenWrite(newfilename))
158
using (FileStream file = File.OpenRead(filename))
160
using (GZipStream gzstream = new GZipStream(file, CompressionMode.Decompress))
162
byte[] buffer = new byte[1024];
164
while ((bytesRead = gzstream.Read(buffer, 0, buffer.Length)) != 0)
166
file2.Write(buffer, 0, bytesRead);
172
filename = newfilename;
173
finfo = new FileInfo(filename);
174
this.util.Message("done", true);
176
this.util.Message("Uploading URL's to database ... ", false);
177
int length = (int)finfo.Length;
179
this.util.ProgressStart(length, String.Empty);
181
System.Collections.Generic.List<string> records = new System.Collections.Generic.List<string>();
182
System.Collections.Generic.Dictionary<string, string[]> urls = new System.Collections.Generic.Dictionary<string, string[]>();
184
string record, hash1, hash2, record2 = String.Empty;
185
using (StreamReader reader = new StreamReader(filename))
187
using (ParseURLs parseurl = new ParseURLs())
189
while (!reader.EndOfStream)
191
for (int j = 0; j < 25000; j++)
193
record2 = reader.ReadLine();
198
if (records.Contains(record2))
202
if ((record2.StartsWith("www.")) && (records.Contains(record2.Substring(4))))
206
if ((!record2.StartsWith("www.")) && (record2.Contains("www." + record2)))
210
records.Add(record2);
212
//Remove bad URL's and create hash for each
213
foreach (string record3 in records)
215
record = ParseURLs.ParseURL(record3);
217
if (record.Length == 0)
221
hash1 = parseurl.GetHash(record);
222
if (!record.StartsWith("www."))
224
hash2 = parseurl.GetHash("www." + record);
228
hash2 = parseurl.GetHash(record.Substring(4));
230
if (!urls.ContainsKey(record))
232
urls.Add(record, new string[] {hash1, hash2});
234
this.util.Curamount += record3.Length;
238
amount += this.db.InsertURLs(urls, false);
242
amount += this.db.InsertURLs(urls, true);
246
records.TrimExcess();
251
this.util.ProgressStop();
252
this.util.Message(String.Empty, true);
253
this.util.Message(amount.ToString() + " URL's was inserted from " + i.ToString() + " URL's.", true);
257
/// Function dispose unmanaged resources.
259
public void Dispose()
261
if (this.util != null)
265
this.disposed = true;