コード例 #1
0
ファイル: IndexReader.cs プロジェクト: Nangal/lucene.net
        public static void  Main(String[] args)
        {
            System.String filename = null;
            bool extract = false;
            
            foreach (string t in args)
            {
                if (t.Equals("-extract"))
                {
                    extract = true;
                }
                else if (filename == null)
                {
                    filename = t;
                }
            }

            if (filename == null)
            {
                System.Console.Out.WriteLine("Usage: Lucene.Net.Index.IndexReader [-extract] <cfsfile>");
                return ;
            }
            
            Directory dir = null;
            CompoundFileReader cfr = null;
            
            try
            {
                var file = new System.IO.FileInfo(filename);
                System.String dirname = new System.IO.FileInfo(file.FullName).DirectoryName;
                filename = file.Name;
                dir = FSDirectory.Open(new System.IO.DirectoryInfo(dirname));
                cfr = new CompoundFileReader(dir, filename);
                
                System.String[] files = cfr.ListAll();
                System.Array.Sort(files); // sort the array of filename so that the output is more readable
                
                foreach (string t in files)
                {
                    long len = cfr.FileLength(t);
                    
                    if (extract)
                    {
                        System.Console.Out.WriteLine("extract " + t + " with " + len + " bytes to local directory...");
                        IndexInput ii = cfr.OpenInput(t);
                        
                        var f = new System.IO.FileStream(t, System.IO.FileMode.Create);
                        
                        // read and write with a small buffer, which is more effectiv than reading byte by byte
                        var buffer = new byte[1024];
                        int chunk = buffer.Length;
                        while (len > 0)
                        {
                            var bufLen = (int) System.Math.Min(chunk, len);
                            ii.ReadBytes(buffer, 0, bufLen);
                            f.Write(buffer, 0, bufLen);
                            len -= bufLen;
                        }
                        
                        f.Close();
                        ii.Close();
                    }
                    else
                        System.Console.Out.WriteLine(t + ": " + len + " bytes");
                }
            }
            catch (System.IO.IOException ioe)
            {
                System.Console.Error.WriteLine(ioe.StackTrace);
            }
            finally
            {
                try
                {
                    if (dir != null)
                        dir.Close();
                    if (cfr != null)
                        cfr.Close();
                }
                catch (System.IO.IOException ioe)
                {
                    System.Console.Error.WriteLine(ioe.StackTrace);
                }
            }
        }