根据c#中的文件大小将文件列表拆分为多个较小的列表

时间:2015-07-16 14:10:10

标签: c# list

我正在编写一个方法,它将获取大量文件并将它们拆分为包含相等总磁盘空间的较小列表。例如。包含1个100kb文件的列表,另一个包含100个文件的列表,每个文件1kb。

我的代码执行以下操作。如果列表中的所有文件总数超过500kb,我想将此列表拆分为较小的列表。这意味着如果我的总计数为600kb,我将有2个列表。我想为这些列表中的每一个添加300kb(或尽可能接近)的文件。

我编写的代码可以很好地完成这项工作,但是有一种常见类型的方案可能会搞砸了。如果我有99个文件。 99是每个1kb,最后一个文件是400kb。此代码将来回添加1个文件到每个列表,直到两个列表在每个列表中将有49个文件和49kb,但现在最终文件很大将意味着1个列表将是49kb而另一个是449kb。我需要一种智能的方法来划分文件,以便400kb文件自己最终列在一个列表中。

int listcount = (int)Math.Ceiling(totalsize / listlimit); //500kb

    List<string>[] lists = new List<string>[listcount];
    double[] memorytotals = new double[listcount]; // this array will keep track of what the file size total is in each of the arrays.

    foreach(string file in filelist)
    {
        double size = new FileInfo(file).Length;

        int pos = 0;
        for (int i = 0; i < memorytotals.Length; i++)
    {
        if (memorytotals[i] < memorytotals[pos]) { pos = i; }
    }
       if(size > memorytotals[pos])
        {
            //get the next smallest array that is not pos
            int pos2 = 0;
            for (int i = 0; i < memorytotals.Length; i++)
            {
                if (memorytotals[i] < memorytotals[pos2] && pos2 != pos) 
                { 
                    pos2 = i; 
                }
            }

            //if moving all contents of the smallest array into the second smallest array make for a smaller size than just putting the larger file directly into the smaller array than do it.
            double newlistTotal = memorytotals[pos] + memorytotals[pos2];
            if(newlistTotal < size)
            {
                lists[pos2].AddRange(lists[pos]);
                //empty the list in order to add the new larger file to this list.
                lists[pos].Clear();
            }
        }
        lists[pos].Add(file);
    }

2 个答案:

答案 0 :(得分:1)

这不是最佳解决方案,但至少它将文件拆分为不同的列表,大小相同。在代码中可以做很多改进,这只是第一种方法。

我订购文件因为它们的大小,然后我开始将它们添加到列表中,检查限制是否永远不会超过。

int listcount = (int)Math.Ceiling(totalsize / listlimit); //500kb
            List<FileInfo> fileInfoList = new List<FileInfo>();

            List<string>[] lists = new List<string>[listcount];

            double[] memorytotals = new double[listcount]; // this array will keep track of what the file size total is in each of the arrays.

            foreach (string file in filelist)
            {
                fileInfoList.Add(new FileInfo(file));         // Add all the FileInfo to a list to order it                      
            }

            fileInfoList.OrderBy(r => r.Length);


            foreach (FileInfo fileInfo in fileInfoList)
            {
                double size = fileInfo.Length;

                // flag for only add a file one time
                bool flag = true;


                for (int j = 0; j < memorytotals.Length; j++)
                {

                    // check if the file fits in the list
                    if (memorytotals[j] + size < listcount && flag)
                    {
                        memorytotals[j] = memorytotals[j] + size;
                        lists[j].Add(fileInfo.FullName);
                        flag = false;
                    }
                }
            }

答案 1 :(得分:0)

我已经编写了一个打包文件的方法,我使用int来替换FileInfo,它可以更容易测试,当然,这不是一个好的实现

static List<List<int>> SplitFileWithLimitSize(List<int> totalSizes, int limitSize)
{
    List<List<int>> resultList = new List<List<int>>();//the result packet
    List<int> tmp = new List<int>();
    int reduceSize = limitSize;
    while (true)
    {
        var maxSize = 0;
        var filters = totalSizes.Where(x => x <= reduceSize);//to fiter the possible size
        if (filters.Any())
        {
            maxSize = filters.Max();//get max size
        }
        if (maxSize == 0)
        {//there is no size got success ,so add the tmp to resultList,and reinit the parameters
            resultList.Add(tmp);
            tmp = new List<int>();
            reduceSize = limitSize;
            continue;
        }
        reduceSize = reduceSize - maxSize;
        totalSizes.Remove(maxSize);
        tmp.Add(maxSize);
        if (totalSizes.Count == 0)
        {//if there is nothing reduce in totalSizes,tmp to resultList,and  break the loop
            resultList.Add(tmp);
            break;
        }
    }
    //resultList.ForEach(x =>
    //{
    //    Console.WriteLine("Pack:" + string.Join(" ", x));
    //});
    return resultList;
}

测试方法是

var totalSizes = new List<int>() { 400, 200, 290, 47, 63 };
var limitSize = 500;
SplitFileWithLimitSize(totalSizes, limitSize);//there will 3 packet in list
Console.WriteLine("#################");
totalSizes = new List<int>() { 400, 200, 290, 100, 10 };
SplitFileWithLimitSize(totalSizes, limitSize);//there will 2 packet in list