//Determine if the file is a RAR or ZIP, or something else public static string GetArchiveType(string filename) { //Try to open it as a RAR try { Unrar unrar = new Unrar(); unrar.Open(filename, Unrar.OpenMode.List); unrar.Close(); return(".cbr"); } catch { //Try to open it as a ZIP try { ZipFile zipFile = new ZipFile(filename); zipFile.Close(); return(".cbz"); } catch { //Failed to open as RAR or ZIP, must be something else FileInfo fileInfo = new FileInfo(filename); string extension = fileInfo.Extension; if (extension != ".cbr" && extension != ".cbz") { return(extension); } //If RAR and ZIP failed to open it, but its extention is CBR or CBZ, then we know that's a lie //And return nothing. } } return(""); }
public Exception Process() { FileStream fileStream = new FileStream(FileName, FileMode.Open); FileSize = (int)fileStream.Length; MD5 = CC.md5file(fileStream); fileStream.Close(); int num = -1; if (Settings.Default.FindDuplicates) { num = FindDuplicate(); } if (num == -1) { string archiveType; try { archiveType = CC.GetArchiveType(FileName); } catch (Exception result) { return result; } string text = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "CarbonImport"); if (!Directory.Exists(text)) { Directory.CreateDirectory(text); } SaveChanges(); ArrayList arrayList = new ArrayList(); try { if (archiveType == ".cbr") { Unrar unrar = new Unrar(); unrar.ArchivePathName = FileName; unrar.Open(Unrar.OpenMode.List); while (unrar.ReadHeader()) { if (!unrar.CurrentFile.IsDirectory & CC.IsImageExt(unrar.CurrentFile.FileName)) { arrayList.Add(unrar.CurrentFile.FileName); } unrar.Skip(); } unrar.Close(); arrayList.Sort(); unrar.Open(Unrar.OpenMode.Extract); while (unrar.ReadHeader()) { if (unrar.CurrentFile.FileName == (string)arrayList[0]) { if (Settings.Default.ThumbGen) { text = Path.Combine(text, ID + Path.GetExtension(unrar.CurrentFile.FileName)); unrar.Extract(text); } break; } unrar.Skip(); } unrar.Close(); } else if (archiveType == ".cbz") { ZipFile zipFile = new ZipFile(FileName); foreach (ZipEntry item in zipFile) { if (item.IsFile & CC.IsImageExt(item.Name) && item.Name.Substring(0, 1) != "." && !item.Name.Contains("/") && !item.Name.Contains("/.")) { arrayList.Add(item.Name); } } zipFile.Close(); arrayList.Sort(); if (Settings.Default.ThumbGen) { text = Path.Combine(text, ID + Path.GetExtension((string)arrayList[0])); CC.unzip(FileName, (string)arrayList[0], text); } } if ((Path.GetExtension(text).ToLower() == ".png") | (Path.GetExtension(text).ToLower() == ".gif")) { text = CC.ConvertJPG(text); } Pages = arrayList.Count; SaveChanges(); } catch (Exception result2) { return result2; } if (Settings.Default.OrganizeMethod != 0) { OrganizeFile(); } if ((archiveType != ".pdf") & Settings.Default.ThumbGen) { try { String dst = Path.Combine(Application.StartupPath, Settings.Default.CoverDir) + "\\" + ID + ".jpg"; CC.CreateThumbnail(text, dst); File.Delete(text); } catch (Exception result3) { return result3; } } } return null; }